Home > Enterprise >  Left zero padded format numbers
Left zero padded format numbers

Time:04-01

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("d", Integer.parseInt(something));

but when I format 124, I get 00124

CodePudding user response:

Try d 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("d", Integer.parseInt(something));
  • Related