NumberFormat Class

NumberFormat Class

In this tutorial, we are going to discuss about NumberFormat Class in java. We can use this class for formatting the numbers according to a particular Locale. This class is available in java.text package and it is an abstract class.

NumberFormat Class

Hence we can’t create an object by using constructor.

NumberFormat nf=new NumberFormat(); // invalid

NumberFormat class contains the following factory methods to get NumberFormat Object.

Important methods
public static NumberFormat getInstance();
public static NumberFormat getCurrencyInstance();
public static NumberFormat getPercentInstance();
public static NumberFormat getNumberInstance();

Getting NumberFormat Object for a particular locale we have to pass the corresponding locale object as the argument to the above methods.

public static NumberFormat getCurrencyInstance(Locale loc)

NumberFormat class contains the following methods for converting a java number to the Locale Specific number form.

String format(long l)
String format(double d) 

NumberFormat class contains the following method for converting Locale specific form to java NumberForm.

Number parse(String s) throws parseException

Consider the java number 123456.789 represents this no in ITALY, US, UK, Specific Forms.

package com.ashok.internationalization;

import java.text.NumberFormat;
import java.util.Locale;
 * @author ashok.mariyala
public class MyNumberFormat {
   public static void main(String[] args) {
      double d1 = 123456.789;
      Locale india = new Locale("pa", "IN");
      NumberFormat nf = NumberFormat.getCurrencyInstance(india);
      System.out.println("India Notation is --->" + nf.format(d1));
      NumberFormat nf1 = NumberFormat.getCurrencyInstance(Locale.ITALY);
      System.out.println("Italy Notation is --->" + nf1.format(d1));
      NumberFormat nf2 = NumberFormat.getCurrencyInstance(Locale.UK);
      System.out.println("UK Notation is --->" + nf2.format(d1));
      NumberFormat nf3 = NumberFormat.getCurrencyInstance(Locale.US);
      System.out.println("US Notation is --->" + nf3.format(d1));


India Notation is --->INR 123,456.79
Italy Notation is --->123.456,79
UK Notation is --->£123,456.79
US Notation is --->$123,456.79
Setting max/min integer and fraction digits

NumberFormat class contains the following methods for specifying max and min fraction and integer digits.

public void setMaximamIntegerDigits(int n);
public void setMinimumIntegerDigits(int n);
public void setMaximamFractionDigits(int n);
public void setMinimumFractionDigits(int n);


NumberFormat nf = NumberFormat.getInstance();

Case 1

System.out.println(nf.format(123456.789)); // 3,456.789

Case 2

System.out.println(nf.format(12.456)); // 0012.456

Case 3

System.out.println(nf.format(123456.789)); // 123,456.79

Case 4

System.out.println(nf.format(123.4)); // 123.400

That’s all about the NumberFormat class in java. If you have any queries or feedback, please write us email at Enjoy learning, Enjoy Java.!!

NumberFormat Class
Scroll to top