Java NumberFormat.getInstance() method


The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country.

To get the instance of the NumberFormat class, call getInstance() method in Java.

First, we will use the getInstance() method.

NumberFormat n = NumberFormat.getInstance();

Now, we will format a double.

double val = 1.9898798;
String formattedVal = n.format(val);

The following is the final example.

Example

 Live Demo

import java.text.NumberFormat;
public class Demo {
    public static void main(String args[]) {
       NumberFormat n = NumberFormat.getInstance();
       double val = 1.9898798;
       System.out.println("Value: "+val);
       String formattedVal = n.format(val);
       System.out.println("Formatted Value: "+formattedVal);
    }
}

Output

Value: 1.9898798
Formatted Value: 1.99

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

338 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements