Set format to two decimal places: set Maximum Fraction Digits in Java

Description

The following code shows how to set format to two decimal places: set Maximum Fraction Digits.

Example


/*from   w w  w. ja v  a2s  .c  o  m*/
import java.text.NumberFormat;

public class Main {
  public static void main(String args[]) {
    NumberFormat nf = NumberFormat.getInstance();

    System.out.println("Default format: " + nf.format(1234567.678));

    nf.setMaximumFractionDigits(2);

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

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter