Right padding with zeros in Java

You could use:

String.format("%-5s", price ).replace(' ', '0')

Can I do this using only the format pattern?

String.format uses Formatter.justify just like the String.printf method.
From this post you will see that the output space is hard-coded, so using the String.replace is necessary.

Leave a Comment