Java Data Type How to - Set total width and decimal width for float point value








Question

We would like to know how to set total width and decimal width for float point value.

Answer

import java.util.Formatter;
// ww w.java 2s .  c o m
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();

    fmt.format("|%10.2f|", 123.123);
    System.out.println(fmt);
  }
}

The code above generates the following result.