NumberFormat
is the abstract base class for all numeric formats. This class provides an interface for formatting and parsing numeric values. NumberFormat
It also provides methods to determine which locales have numeric formatting, and what their names are.
NumberFormat
Numeric values that can be used to format and parse any locale. Enables the code to be completely independent of the locale conventions of the decimal point, thousands separator, or even the specified number of decimal places, regardless of whether the numeric format is even decimal.
To format the values of the current Locale, you can use one of the factory class methods:
If you format multiple numeric values, it is more efficient to get the format and use it multiple times, so that the system does not have to get information about the language and country/region conventions more than once.
NumberFormat NF = numberformat.getinstance (); for (int i = 0; i < mynumber.length; ++i) {
To format a date for a different Locale, you can specify it in the invocation of the pair getInstance
.
You can also use NumberFormat
to parse values:
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 locale. String myString = numberformat.getinstance (). Format (MyNumber); System.out.println (myString); Getcurrencyinstance () returns the currency format of the current default locale. myString = Numberformat.getcurrencyinstance (). Format (MyNumber); System.out.println (myString); Getnumberinstance () returns the general numeric format for the current default locale. myString = Numberformat.getnumberinstance (). Format (MyNumber); System.out.println (myString); Getpercentinstance () returns the percentage format for the current default locale. myString = Numberformat.getpercentinstance (). Format (test); System.out.println (myString); NumberFormat format = Numberformat.getinstance (); setminimumfractiondigits (int) Sets the minimum number of digits allowed for the decimal part of a numeric value. Format.setminimumfractiondigits (3); setmaximumfractiondigits (int) Sets the maximum number of digits allowed for the decimal portion of a number. Format.setmaximumfRactiondigits (5); setmaximumintegerdigits (int) Sets the maximum number of digits allowed for an integer portion of a numeric value. Format.setmaximumintegerdigits (10); setminimumintegerdigits (int) Sets the minimum number of digits allowed for an integer portion of a numeric value. Format.setminimumintegerdigits (1); Set the Roundingmode used in this numberformat. Format.setroundingmode (ROUNDINGMODE.HALF_UP); System.out.println (Format.format (0.2321222));}}
Java.text.NumberFormat