![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
Advertisements