Java Data Type How to - Set precision modifier for double value to keep 4 decimal places








Question

We would like to know how to set precision modifier for double value to keep 4 decimal places.

Answer

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

    fmt.format("%.4f", 123.1234567);
    System.out.println(fmt);
  }
}

The code above generates the following result.