NumberFormat class in Java
NumberFormat is an abstract base class for all number formats. This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales (US, India, Italy, etc) have number formats, and what their names are. NumberFormat helps you to format and parse numbers for any locale.
Example: Suppose we have a double type number. But this double type number is represented in different ways in different Countries. To represent a number according to various countries we have to take the help of NumberFormat class like:
double d = 123456.789; For India, it is represented like 1,23,456.789 For US, it is represented like 123,456.789 For ITALY, it is represented like 123.456,789
Some important points about NumberFormat class:
- NumberFormat class is present in java.text package and it is an abstract class.
- NumberFormat class implements Serializable, Cloneable.
- NumberFormat is the direct child class of Format class.
- Number formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
Methods present in NumberFormat class:
- public static NumberFormat getInstance();: To get the NumberFormat object for default Locale.
- public static NumberFormat getCurrencyInstance();: To get the NumberFormat object for default Locale to represent in specific Currency.
- public static NumberFormat getPercentInstance();:
- public static NumberFormat getInstance(Locale l);: To get the NumberFormat object for the specified Locale object.
- public static format(long l);:To convert java number to locale object.
// Java Program to illustrate NumberFormat class use import java.util.*; import java.text.*; class NumberFormatDemo { public static void main(String[] args) { double d = 123456.789 ; NumberFormat nf = NumberFormat.getInstance(Locale.ITALY); System.out.println( "ITALY representation of " + d + " : " + nf.format(d)); } } |
Output:
ITALY representation of 123456.789 : 123.456, 789
Recommended Posts:
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.lang.Character.Subset Class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.