Java Data Type How to - Set format to two decimal places: set Minimum Fraction Digits








Question

We would like to know how to set format to two decimal places: set Minimum Fraction Digits.

Answer

import java.text.NumberFormat;
//  ww w.ja v a2 s.c o  m
public class Main {
  public static void main(String[] argv) throws Exception {

    NumberFormat nf = NumberFormat.getInstance();

    nf.setMinimumFractionDigits(2);

    System.out.println("Format with two decimal places: " + nf.format(1234567.678));
    System.out.println("Format with two decimal places: " + nf.format(1234567.6));

  }
}

The code above generates the following result.