Java. text. NumberFormat
NumberFormat
Is the abstract base class of all numeric formats. This class provides interfaces for formatting and parsing numeric values.NumberFormat
Some methods are provided to determine which language environments have numerical formats and what their names are.
NumberFormat
It can be used to format and parse numeric values in any language environment. This allows the code to be completely independent from the language conventions of the decimal point, thousands of delimiters, or even specific decimal places, and has nothing to do with whether the numeric format is an even decimal.
To format the current value of Locale, you can use one of the factory class methods:
myString = NumberFormat.getInstance().format(myNumber);
If you format multiple values, it is more efficient to obtain the format and use it multiple times, in this way, the system does not have to obtain information about language and environment languages and national/regional agreements for multiple times.
NumberFormat nf = NumberFormat.getInstance(); for (int i = 0; i < myNumber.length; ++i) { output.println(nf.format(myNumber[i]) + "; "); }
To format the date of different Locale, you cangetInstance
It is specified in the call.
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
You can also useNumberFormat
To parse the value:
myNumber = nf.parse(myString);
Import java. math. roundingMode; import java. text. decimalFormat; import java. text. numberFormat; public class TestClass {/*** @ param args */public static void main (String [] args) {Double myNumber = 23323.3325632323; Double test = 0.3464; // getInstance () // returns the General numeric format of the current default language environment. String myString = NumberFormat. getInstance (). format (myNumber); System. out. println (myString); // getCurrencyInstance () returns the currency format of the current default language environment. MyString = NumberFormat. getCurrencyInstance (). format (myNumber); System. out. println (myString); // getNumberInstance () returns the general value format of the current default language environment. MyString = NumberFormat. getNumberInstance (). format (myNumber); System. out. println (myString); // getPercentInstance () returns the percentage format of the current default language environment. MyString = NumberFormat. getPercentInstance (). format (test); System. out. println (myString); NumberFormat format = NumberFormat. getInstance (); // setMinimumFractionDigits (int) sets the minimum number of digits allowed in the decimal part of the value. Format. setMinimumFractionDigits (3); // setMaximumFractionDigits (int) sets the maximum number of digits allowed in the fractional part of the value. Format. setMaximumFractionDigits (5); // setMaximumIntegerDigits (int) sets the maximum number of digits allowed for the integer part of the value. Format. setMaximumIntegerDigits (10); // setMinimumIntegerDigits (int) sets the minimum number of digits allowed in the integer part of the value. format. setMinimumIntegerDigits (1); // set the RoundingMode used in this NumberFormat. Format. setRoundingMode (RoundingMode. HALF_UP); System. out. println (format. format (0.2321222 ));}}