1.NumberFormat represents a formatted class of numbers NumberFormat a format class that represents numbers, that is, a number can be displayed according to local style habits.
No. |
Method |
Type |
Describe |
1 |
public static locale[] getavailablelocales () |
Ordinary |
Returns an array of all locales |
2 |
public static final NumberFormat getinstance () |
Ordinary |
Returns the number format for the current default locale |
3 |
public static NumberFormat getinstance (Locale inlocale) |
Ordinary |
Returns the number format for the specified locale |
4 |
public static final NumberFormat getcurrencyinstance () |
Ordinary |
Returns the general format of the current default locale |
5 |
public static NumberFormat getcurrencyinstance (Locale inlocale) |
Ordinary |
Returns the number format for the specified locale |
private static final numberformat CURRENCY = Numberformat.getcurrencyinstan CE ();
Package Com.pb.demo1; Import Java.text.NumberFormat; Public class numberformattest { publicstaticvoid main (string[] args) { NumberFormat NF=numberformat.getinstance (); System.out.println ("show numbers after formatting:" +nf.format (10000000)); System.out.println ("show numbers after formatting:" +nf.format (10000.345));} }
2.DecimalFormat formatted digital DecimalFormat is also a subclass of format, the main function is to format the numbers, of course, in the format of the numbers than the direct use of numberformat more convenient, Because you can directly specify a user-defined way of formatting, similar to SimpleDateFormat, you must specify a template for the formatting action if you want to perform a custom format operation.
No. |
Mark |
Position |
Describe |
1 |
0 |
Digital |
Represents an Arabic numeral, each 0 represents an Arabic numeral, if the bit does not exist the display 0 |
2 |
# |
Digital |
Represents Arabic numerals, each of which represents an Arabic numeral, and does not appear if the bit does not exist |
3 |
. |
Digital |
Decimal separator or decimal separator for currency |
4 |
- |
Digital |
Represents the minus sign |
5 |
, |
Digital |
Grouping separators |
6 |
E |
Digital |
Dividing the mantissa and exponent in the scientific counting method |
7 |
; |
Child mode Boundaries |
Separating positive and negative sub-patterns |
8 |
% |
Prefix or suffix |
Number multiplied by 100 and displayed as a percentage |
9 |
\u2030 |
Prefix or suffix |
Multiplied by 1000 and displayed as thousand fractions |
10 |
¤\u00a4 |
Prefix or suffix |
Currency symbol, replaced by currency number. If two appears simultaneously, it is replaced with an international currency symbol. If it appears in a pattern, the currency decimal separator is used instead of the decimal separator character. |
11 |
, |
Prefix or suffix |
Used to multibyte quotation marks for special words in a prefix or suffix, such as "# ' #" to format 123 as "#123". To create a single quotation mark itself, use two single quotes consecutively: "# o ' Clock". |
PackageCom.pb.demo1;ImportJava.text.DecimalFormat; Public classFormatdemo { Public voidFormat (String pattern,Doublevalue) {DecimalFormat DF=NewDecimalFormat (pattern); String Str=Df.format (value); System.out.println ("Use" + pattern+ "\ t format number" +value+ ": \ T" +str); } Public Static voidMain (string[] args) {Formatdemo demo=NewFormatdemo (); Demo.format ("###,###.###", 111222.34567); Demo.format ("000,000.000", 11222.34567); Demo.format ("###,###.###$", 111222.34567); Demo.format ("000,000.000¥", 11222.34567); Demo.format ("##.###%", 0.345678);//use a percentage formDemo.format ("00.###%", 0.0345678);//use a percentage formDemo.format ("###.###\u2030", 0.345678);//use thousands of fractional forms }}
Java-numberformat and DecimalFormat class