Specify the number of digits in java

Suppose you have:

int a = 3;

      

And I want to indicate that the number of digits is 5. So when I print it, the output is:

00003

      

Thank!

+3


source to share


1 answer


An int

only stores the value, no formatting. To show or store leading zeros, you need to use a string.



 System.out.println(String.format("%05d", a));

      

+11


source







All Articles