Set a Minimum Field Width in Java

Java 8Object Oriented ProgrammingProgramming

The minimum field width specifier is what you include between % and the format conversion code.

To include a 0 before the field width specifier, pad with 0's. An integer between the % sign and the format conversion code acts as a minimum field width specifier.

Let us see an example −

Example

 Live Demo

import java.util.Formatter;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      System.out.println(f.format("%08d", 697));
      f = new Formatter();
      System.out.println(f.format("%10d", 9878));
      f = new Formatter();
      System.out.println(f.format("%06d", 697));
   }
}

Output

00000697
     9878
000697
raja
Published on 21-Dec-2018 12:08:53
Advertisements