Set format to two decimal places : NumberFormat « I18N « Java Tutorial






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));

  }
}
/*
Default format: 1,234,567.678
Format with two decimal places: 1,234,567.68
*/








13.14.NumberFormat
13.14.1.Format for GERMAN locale
13.14.2.Format for the default locale
13.14.3.Parse a GERMAN number
13.14.4.Formatting a Number in Exponential Notation
13.14.5.Using only 0's to the left of E forces no decimal point
13.14.6.Formatting and Parsing Locale-Specific Currency
13.14.7.Parse number with NumberFormat and Locale
13.14.8.Formatting and Parsing a Locale-Specific Percentage
13.14.9.Parse a number with NumberFormat and Locale.CANADA
13.14.10.Format a number with DecimalFormat
13.14.11.Parse a number for a locale
13.14.12.Format a number for a locale
13.14.13.Use java.text.NumberFormat to format a currency value.
13.14.14.Use grouping to display a number
13.14.15.Set format to two decimal places