Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I try to format integers so they always have four proper digits, that is, I want 0s for padding.
I have tried "%04d" which works nicely for positive number. For negatives it gives things like -[854] where I need -[0854].
"%04d"
-[854]
-[0854]
Thank you.
int i=1;System.out.printf("%04d",i);
"% 05d"
if(number>=0){ String.format("%04d",number); }else{ String.format("%05d",number); }
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
int i=1;System.out.printf("%04d",i);instead of just"%04d")"% 05d", see the 4th flag here)