0

I want to format 4-digit numbers with left padded zeros and 5-digit numbers with no leading zeros so when e.g:

  • I insert 124 it is formatted to 0124.
  • 30 is formatted to 0030.
  • When number >9999 then it has no zero padding.

I tried String.format("%05d", Integer.parseInt(something));

but when I format 124, I get 00124

komplRX
  • 5
  • 6

1 Answers1

1

Try %04d instead:

  • 0 indicates what you're using to pad;
  • 4 is the width of the number (you had 5 before);
  • d represents an integer (incl. byte, short, int, long, bigint).
String.format("%04d", Integer.parseInt(something));
happy songs
  • 835
  • 8
  • 21