DecimalFormat is a concrete subclass of NumberFormat for formatting decimal numbers. This class is designed with various functions that enable it to parse and format numbers in any locale, including support for Western languages, Arabic, and Indic numbers. It also supports different types of numbers, including whole numbers (123), fixed-point numbers (123.4), numbers in scientific notation (1.23E4), percentages (12%), and amounts ($123). All of this content can be localized.
12.3
12.3
012.340
12.34
1111,1112,3456.123
111,111,123,456.123
111,111,123,456.123
1.000E0004
1.235E0007
30.522%
If you use a pattern with multiple grouping characters, the interval between the last separator and the end of the integer is the grouping size used. So "#,##,###,####" == "######,####" == "##,####,####".
DecimalFormat contains a pattern and a set of symbols
Symbol meaning:
symbol | Location | localized? | meaning |
0 |
number | Yes | Arabic numerals |
# |
digital word | Yes | Arabic numerals, empty if not present |
. |
number | Yes | Decimal Separator or Currency Decimal Separator |
- |
number | Yes | minus sign |
, |
number | Yes | grouping separator |
E |
number | Yes | Separates the mantissa and exponent in scientific notation. No quotation marks are required in the prefix or suffix. |
; |
subpattern boundaries | Yes | Separate positive and negative subpatterns |
% |
prefix or suffix | Yes | Multiply by 100 and display as a percentage |
/u2030 |
prefix or suffix | Yes | Multiply by 1000 and display as thousandths |
¤ (/u00A4 ) |
prefix or suffix | no | Currency symbol, replaced by currency symbol. If both are present, they are replaced with the international currency symbol. If present in a pattern, the currency decimal separator is used instead of the decimal separator. |
' |
prefix or suffix | no | Used to quote special characters in prefix or or suffix, for example "'#'#" to format 123 as "#123" . To create a single quote itself, use two single quotes in a row: "# o''clock" . |
example:
运行结果:
12.3
12.3
012.340
12.34
Results:
1111,1112,3456.123
111,111,123,456.123
111,111,123,456.123
1.000E0004
1.235E0007
30.522%
If you use a pattern with multiple grouping characters, the interval between the last separator and the end of the integer is the grouping size used. So "#,##,###,####" == "######,####" == "##,####,####".