Set a Minimum Field Width in Java


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements