Java Format Number using NumberFormat class

Tags: java.text.NumberFormat Java Format Number

In this Java tutorial we learn how to use the java.text.NumberFormat class to format different type of numbers in different locales in Java application.

Table of contents

  1. How to use java.text.NumberFormat class
  2. More java.text.NumberFormat Java Examples

How to use java.text.NumberFormat class

Firstly we need to get instance of NumberFormat class using provided methods.

For example, the following Java program to format number with current default locale we use the NumberFormat.getInstance() method.

NumberFormatExample1.java

import java.text.NumberFormat;

public class NumberFormatExample1 {
    public static void main(String... args) {
        long longValue = Long.MAX_VALUE;
        int intValue = Integer.MAX_VALUE;
        double doubleValue = 9999999.987654321;
        float floatValue = 54321.12345F;

        NumberFormat numberFormat = NumberFormat.getInstance();

        String formattedLongValue = numberFormat.format(longValue);
        String formattedIntValue = numberFormat.format(intValue);
        String formattedDoubleValue = numberFormat.format(doubleValue);
        String formattedFloatValue = numberFormat.format(floatValue);

        System.out.println("Formatted long value: " + formattedLongValue);
        System.out.println("Formatted int value: " + formattedIntValue);
        System.out.println("Formatted double value: " + formattedDoubleValue);
        System.out.println("Formatted float value: " + formattedFloatValue);
    }
}
The output as below.
Formatted long value: 9,223,372,036,854,775,807
Formatted int value: 2,147,483,647
Formatted double value: 9,999,999.988
Formatted float value: 54,321.125

We also can format number with given specified Locale as following Java application.

NumberFormatExample2.java

import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample2 {
    public static void main(String... args) {
        long longValue = 223344556677L;

        NumberFormat numberFormat = NumberFormat.getInstance(Locale.GERMANY);

        String formattedValue = numberFormat.format(longValue);

        System.out.println("Long value format in Germany: " + formattedValue);
    }
}
The output as below.
Long value format in Germany: 223.344.556.677

More java.text.NumberFormat Java Examples

Format Number with custom Locale

The following Java program to show you how to format number with NumberFormat class with a specified Locale.

NumberFormatExample3.java

import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample3 {
    public static void main(String... args) {
        long longValue = Long.MAX_VALUE;

        NumberFormat usNumberFormat = NumberFormat.getInstance(new Locale("en", "US"));
        NumberFormat daNumberFormat = NumberFormat.getInstance(new Locale("da", "DK"));

        System.out.println(usNumberFormat.format(longValue));
        System.out.println(daNumberFormat.format(longValue));
    }
}
The output as below.
9,223,372,036,854,775,807
9.223.372.036.854.775.807

Format Number with and without grouping separator

We can set whether or not to use the grouping separator to format number by using the NumberFormat.setGroupingUsed() method as following Java program.

NumberFormatExample4.java

import java.text.NumberFormat;

public class NumberFormatExample4 {
    public static void main(String... args) {
        long longValue = 123456789L;

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setGroupingUsed(true);

        System.out.println("Format with grouping: ");
        System.out.println(numberFormat.format(longValue));


        numberFormat.setGroupingUsed(false);

        System.out.println("Format without grouping: ");
        System.out.println(numberFormat.format(longValue));
    }
}
The output as below.
Format with grouping: 
123,456,789
Format without grouping: 
123456789

Format Number with number of fraction digits

To format the double or float value we an use setMinimumFractionDigits() and setMaximumFractionDigits() methods to set minimum and maximum number of fraction digits as below.

NumberFormatExample5.java

import java.text.NumberFormat;

public class NumberFormatExample5 {
    public static void main(String... args) {
        double doubleValue = 34.2222222222;
        float floatValue = 99.9F;

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMinimumFractionDigits(3);
        numberFormat.setMaximumFractionDigits(5);

        System.out.println(numberFormat.format(doubleValue));
        System.out.println(numberFormat.format(floatValue));
    }
}
The output as below.
34.22222
99.900

Format Number with rounding mode

In the following Java program we use NumberFormat.setRoundingMode() method to set different rounding modes during format double values.

NumberFormatExample6.java

import java.math.RoundingMode;
import java.text.NumberFormat;

public class NumberFormatExample6 {
    public static void main(String... args) {
        double doubleValue1 = 34.22222;
        double doubleValue2 = 34.55555;

        NumberFormat numberFormat = NumberFormat.getInstance();

        numberFormat.setRoundingMode(RoundingMode.UP);
        System.out.println("Format with rounding mode UP");
        System.out.println(numberFormat.format(doubleValue1));
        System.out.println(numberFormat.format(doubleValue2));

        System.out.println("Format with rounding mode DOWN");
        numberFormat.setRoundingMode(RoundingMode.DOWN);
        System.out.println(numberFormat.format(doubleValue1));
        System.out.println(numberFormat.format(doubleValue2));

        System.out.println("Format with rounding mode CEILING");
        numberFormat.setRoundingMode(RoundingMode.CEILING);
        System.out.println(numberFormat.format(doubleValue1));
        System.out.println(numberFormat.format(doubleValue2));

        System.out.println("Format with rounding mode FLOOR");
        numberFormat.setRoundingMode(RoundingMode.FLOOR);
        System.out.println(numberFormat.format(doubleValue1));
        System.out.println(numberFormat.format(doubleValue2));
    }
}
The output as below.
Format with rounding mode UP
34.223
34.556
Format with rounding mode DOWN
34.222
34.555
Format with rounding mode CEILING
34.223
34.556
Format with rounding mode FLOOR
34.222
34.555

Format Percentage number

To format a percentage number in Java program we can NumberFormat.getPercentInstance() method to get the NumberFormat instance as below.

NumberFormatExample7.java

import java.text.NumberFormat;

public class NumberFormatExample7 {
    public static void main(String... args) {
        double doubleValue1 = 16f/100f;
        double doubleValue2 = 0.35;

        NumberFormat numberFormat = NumberFormat.getPercentInstance();

        System.out.println(numberFormat.format(doubleValue1));
        System.out.println(numberFormat.format(doubleValue2));
    }
}
The output as below.
16%
35%

Format Currency Number

To format a currency number in Java we can use the NumberFormat.getCurrencyInstance() method to get instance of NumberFormat as following Java example.

NumberFormatExample8.java

import java.text.NumberFormat;

public class NumberFormatExample8 {
    public static void main(String... args) {
        double doubleValue = 0.356;

        NumberFormat numberFormat = NumberFormat.getCurrencyInstance();

        System.out.println(numberFormat.format(doubleValue));
    }
}
The output as below.
$0.36

Below Java program to show you how to format currency with given locale.

NumberFormatExample9.java

import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample9 {
    public static void main(String... args) {
        int intValue = 30000;

        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale("vn", "VN"));

        System.out.println(numberFormat.format(intValue));
    }
}
The output as below.
VND 30,000

Happy Coding 😊

Java Parse String to Number using NumberFormat class