How to add leading zeros to Integers in Java – String left padding Example Program

From Java 5 onwards it's easy to left pad Integers with leading zeros when printing number as String. In fact, You can use the same technique to left and right pad Java String with any characters including zeros, space. Java 5 provides String format methods to display String in various format. By using format() method we can add leading zeros to any Integer or String number. By the way, this is also known as left padding Integers with zero in Java. For those who are not familiar with left and right padding of String, here is a quick recap; When we add characters like zero or space on left of a String, its known as left padding and when we do the same on the right side of String, it's known as right padding of String. This is useful when you are displaying currency amounts or numbers, which has fixed width, and you want to add leading zeros instead of space in front of Integers.
Read more »