By the given task, the numbers can be 000, 001, 002 and so on. How can I save this number format?
int i = 000;
i++;
System.out.println(i); //the output is 1 but I want 001
So how can I have the value of i
like 001
?
By the given task, the numbers can be 000, 001, 002 and so on. How can I save this number format?
int i = 000;
i++;
System.out.println(i); //the output is 1 but I want 001
So how can I have the value of i
like 001
?
Store the original value in a String.
Try this: String originalValue = "000";
To display you int with a length of 3 digits, you can use String.format
like this:
System.out.println(String.format("%03d", i));
The language-independent name for what it sounds like you're trying to do is "format".
A lot of languages with C heritage will have a function named something like printf
, fprintf
, or sprintf
, with a common protocol of %
markers for their arguments.