Decimalformat format a number
The decimalformat class is also a subclass of format. It is mainly used to format numbers. Of course, it is more convenient to format a number than to directly use numberformat, because you can directly specify a custom format, similar to simpledateformat. If you want to customize the format, you must specify a formatting template, as shown in table 11-13.
Table 11-13 decimalformat format template
Serial number |
Mark |
Bit |
Description |
1 |
0 |
Number |
Each 0 represents an Arabic number, If this bit does not exist, 0 is displayed. |
2 |
# |
Number |
Represents an Arabic number. Each # represents an Arabic number, If this bit does not exist, it is not displayed. |
3 |
. |
Number |
Decimal separator or decimal separator of currency |
4 |
- |
Number |
Negative number |
5 |
, |
Number |
Group Separator |
6 |
E |
Number |
Separate the ending number and index in scientific notation |
7 |
; |
Submode Boundary |
Separate Positive and Negative submodes |
8 |
% |
Prefix or suffix |
The number is multiplied by 100 and displayed as a percentage. |
9 |
\ U2030 |
Prefix or suffix |
Multiply by 1000 and display as a score |
10 |
Bytes \ U00a4 |
Prefix or suffix |
Currency mark, which is replaced by the currency number. If both of them appear at the same time, It is replaced by the international currency symbol. If it appears in a certain mode , The decimal separator is used instead of the decimal separator. |
11 |
, |
Prefix or suffix |
It is used to enclose special characters with quotation marks in the prefix or suffix, for example & Quot; '# & quot; format 123 as & quot; #123 & quot ". Create a ticket Number itself, two single quotes are used consecutively, for example, "# O '''clock" |
Example:
1 decimalformat format1 = new decimalformat ("000.00"); 2 system. out. println ("test. main () "+ format1.format (12.345); // before and after the decimal point, the number of digits must be zero. after the decimal point, the majority of digits are truncated. after the decimal point is 6, the number is 3 4 decimalformat format2 = new decimalformat ("##. # "); 5 system. out. println ("test. main () "+ format2.format (3.456); // before the decimal point and after the decimal point, do not add 0. after the decimal point, most of them are truncated, 6 into 6 7 decimalformat format3 = new decimalformat ("000,000,000.000"); 8 system. out. println ("test. main () "+ format3.format (43215696.4562286); // delimiter before the number 043,215,696.456 9 10 decimalformat format4 = new decimalformat ("#. # % "); 11 system. out. println ("test. main () "+ format4.format (4.5); // 450% 12 13 decimalformat format5 = new decimalformat ("#. # \ u2030 "); 14 system. out. println ("test. main () "+ format5.format (0.0002); // semicolon 0.2 ‰ 15 16 decimalformat format6 = new decimalformat (" 'This is my money $ '##. # "); // number modifier (separated by'') This is my money $123.217 system. out. println ("test. main () "+ format6.format (123.2); 18
View code
Decimalformat format a number