How to print any number into specific number of digits?
To print any integer in specific number of digits then we can use format() function from java.lang.String class.
For e.g.
public class LeadingZerosExample {
public static void main(String[] args) {
int number = 15;
String formatted = String.format("%04d", number);
System.out.println("Number with leading zeros: " + formatted);
}
}
Output for this program will be as follows:
Number with leading zeros: 0015
Comments
Post a Comment