4

I have a variable saying private int minutes = 00.

If I do System.out.println(minutes), Java will print 0 instead of 00, which is the value. It is removing the last zero, but in this particular example, I want it to print 00. How to do that?

I also tried making minutes a string, and do: Integer.parseInt(minutes) and then print that. But the result is the same.

arshajii
  • 127,459
  • 24
  • 238
  • 287
AomSet
  • 373
  • 4
  • 5
  • 14
  • 1
    Before you start doing things without knowing that you're doing them, if your `int` literal starts with a `0` it is an Octal. – Sotirios Delimanolis Nov 12 '13 at 15:45
  • 1
    This has been asked before http://stackoverflow.com/questions/275711/add-leading-zeroes-to-number-in-java – Andersnk Nov 12 '13 at 15:45
  • @SotiriosDelimanolis, Java's `Integer.parseInt(String s)` method is identical to calling `Integer.parseInt(String s, int radix)` with a radix of 10. `parseInt` with a single argument won't treat the string as an octal number. (That said, prepending an integer _literal_ with 0 will get you octal values.) – Brian S Nov 12 '13 at 15:50

4 Answers4

9

You can use printf():

int n = 00;
System.out.printf("%02d%n", n);
00

But be careful with prepending int literals with a 0, since that makes them octal (see JLS §3.10.1).

For instance,

int n = 010;
System.out.printf("%02d%n", n);
08
arshajii
  • 127,459
  • 24
  • 238
  • 287
8

You can print with a format:

System.out.printf("%02d%n", minutes);

Explanation:

  • The %02d specifies an integer conversion ($d) with a width of 2 and a 0 padding flag.
  • The %n specifies a line terminator (to mimic the new line that println adds)

If you want to convert minutes to a String for use within the program (rather than printing) you can use String.format to the same effect:

int minutes = 0;
String sMinutes = String.format("%02d", minutes);

More information can be found in the docs on Formatter syntax.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
6

You can do the following -

private int minutes = 0
DecimalFormat formatter = new DecimalFormat("00");
String aFormatString = formatter.format(minutes);

System.out.println(aFormatString);

Just saving some lines - System.out.println(new DecimalFormat("00").format(minutes));

JHS
  • 7,761
  • 2
  • 29
  • 53
  • Thanks all. Now I see another problem for me. What I do is set the time like time = hour + ":" + minutes. If the time is 11:00, this will set the time to be 11:0. Can I use the formatting here? – AomSet Nov 12 '13 at 15:47
  • 1
    You can pass the hour also in the formatter. Like - String time = formatter.format(hours) + ":" + formatter.format(minutes). – JHS Nov 12 '13 at 15:50
0

you can try this method, i've learned it in the college

private static String formate(int valor) {
    return (valor < 10 ? "0" : "") + valor;
}
  • 1
    1) It fails for negative numbers. 2) It doesn't scale if you want to multiple leading zeros. Using `printf` is a much better approach. – Stephen C May 30 '19 at 22:57