How to format an integer with four zero left strings?

I am trying to do something like this in java (Eclipse Indigo):

input - 16 (integer);
Output - "0016" ;

input - 201 (integer);
Output - "0201" ;

intput - 1716 (integer);
Output - "1716" ;

In VB.net, I could use:

dim num as integer
dim str as string

str = Format(num, "0000")

How can i do the same in java? Thanks in advance...

+5
source share
2 answers

I would also like to find String#format()how mre answered, but you can also do:

new DecimalFormat("0000").format(number);

Check here for more details.

+3
source

final String s = String.format("%04d", yourNumber).

Note that there is also a reference version of this method.

+8
source

All Articles