DecimalFormat is a specific subclass of NumberFormat that is used to format decimal digits. The class has a variety of features that enable it to analyze and format numbers in any locale, including support for Western, Arabic, and Indic numerals. It also supports different types of numbers, including integers (123), fixed-point numbers (123.4), numbers represented by scientific notation (1.23E4), percentages (12%) and amount ($123). All of this content can be localized.
DecimalFormat contains a pattern and a set of symbols
Symbolic Meaning:
Symbol |
Position |
Localization? |
Meaning |
0 |
Digital |
Is |
Arabic numerals |
# |
Digital word |
Is |
Arabic numerals, shown as empty if not present |
. |
Digital |
Is |
Decimal separator or Currency decimal separator |
- |
Digital |
Is |
Minus sign |
, |
Digital |
Is |
Grouping separators |
E |
Digital |
Is |
Separates the mantissa and exponent in the scientific notation. No quotation marks are required in the prefix or suffix. |
; |
Child mode Boundaries |
Is |
Separating positive and negative sub-patterns |
% |
Prefix or suffix |
Is |
Multiplied by 100 and displayed as a percentage |
/u2030 |
Prefix or suffix |
Is |
Multiplied by 1000 and displayed as thousand fractions |
¤ (/u00A4 ) |
Prefix or suffix |
Whether |
Currency symbol, which is replaced by a currency sign. 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. |
‘ |
Prefix or suffix |
Whether |
Used to multibyte quotation marks for special words in a prefix or suffix, for example, to "‘#‘#" format 123 "#123" . To create a single quotation mark itself, use two single quotes consecutively: "# o‘‘clock" . |
Example:
New DecimalFormat ("0.0"new DecimalFormat ("#.#"new DecimalFormat ("000.000" New DecimalFormat ("###.###"); System.out.println (Df1.format (12.34)); System.out.println (Df2.format (12.34)); System.out.println (Df3.format (12.34)); System.out.println (Df4.format (
Operation Result:
12.3
12.3
012.340
12.34
DecimalFormat format =NewDecimalFormat ("###,####.000"); System.out.println (Format.format (111111123456.1227222)); Locale.setdefault (locale.us); DecimalFormat Usformat=NewDecimalFormat ("###,###.000"); System.out.println (Usformat.format (111111123456.1227222)); DecimalFormat Addpattenformat=NewDecimalFormat (); Addpattenformat.applypattern ("##,###.000"); System.out.println (Addpattenformat.format (111111123456.1227)); DecimalFormat Zhiformat=NewDecimalFormat (); Zhiformat.applypattern ("0.000E0000"); System.out.println (Zhiformat.format (10000)); System.out.println (Zhiformat.format (12345678.345)); DecimalFormat Percentformat=NewDecimalFormat (); Percentformat.applypattern ("#0.0%"); System.out.println (Percentformat.format (0.3052222));
Operation Result:
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 more than one grouping character, the interval between the last delimiter and the end of the integer is the grouping size used. So "#,##,###,####" = = "######,####" = = "##,####,####".
The main functions of Java DecimalFormat and how to use them