NumberFormat Class
Package: java.text
The NumberFormat class provides methods that let you convert numeric values to strings with various types of numeric formatting applied.
Methods
|
Method |
Explanation |
|
|
A static method that returns a |
|
|
A static method that returns a |
|
|
A static method that returns a |
|
|
Returns a string that contains the formatted number. |
|
|
Sets the minimum number of digits to display to the right of the decimal point. |
|
|
Sets the maximum number of digits to display to the right of the decimal point. |
To use the NumberFormat class to format numbers, you must first call one of the static getXxxInstance methods to create a NumberFormat object that can format numbers in a particular way. Then, if you want, you can call the setMinimum FractionDigits or setMaximumFractionDigits method to set the number of decimal digits to be displayed. Finally, you call that object’s format method to actually format a number.
Here’s an example that uses the NumberFormat class to format a double value as currency:
double salesTax = 2.425;
NumberFormat cf = NumberFormat.getCurrencyInstance(); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access