Oliver Hoffman
Oliver Hoffman

Reputation: 550

Specify number of digits in java

Suppose you have:

int a = 3;

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

00003

Thanks!

Upvotes: 3

Views: 1049

Answers (1)

Thilo
Thilo

Reputation: 262534

An int stores only the value, no formatting information. To show or preserve leading zeros you have to use a String.

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

Upvotes: 11

Related Questions