Mistu4u Mistu4u - 1 year ago 145
Java Question

Save an integer in two digit format in a variable in Java

How can I store an integer in two digit format in Java? Like can I set

int a=01;

and print it as
? Also, not only printing, if I say
int b=a;
should also print its value as

Answer Source

I think this is what you're looking for:

int a = 1;
DecimalFormat formatter = new DecimalFormat("00");
String aFormatted = formatter.format(a);


Or, more briefly:

int a = 1;
System.out.println(new DecimalFormat("00").format(a));

An int just stores a quantity, and 01 and 1 represent the same quantity so they're stored the same way.

DecimalFormat builds a String that represents the quantity in a particular format.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download