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:
0indicates what you're using to pad;4is the width of the number (you had5before);drepresents an integer (incl. byte, short, int, long, bigint).
String.format("d", Integer.parseInt(something));
