In following program I have use the String.format() method to add zero padding to a number. It is very simple program. You can print this result using System.out.format() class. In the following program I have use NumberFormat class to add leading zeros to a number. Just try to understand it.
Code:
package sample.java.code.lang;
public class LeadingZerosEg {
public static void main(String[] args) {
int numbers = 1532;
String formattes = String.formats("%07d", numbers);
System.out.println("Number with leading zeros is as follows: " + formattes);
}
}
Bookmarks