The usage of DecimalFormat and the meaning of each symbol

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. 

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:

[java]  view plain  copy
  1. DecimalFormat df1 = new DecimalFormat("0.0");   
  2. DecimalFormat df2 = new DecimalFormat("#.#");   
  3. DecimalFormat df3 = new DecimalFormat("000.000");   
  4. DecimalFormat df4 = new DecimalFormat("###.###");   
  5. System.out.println(df1.format(12.34));   
  6. System.out.println(df2.format(12.34));   
  7. System.out.println(df3.format(12.34));   
  8. System.out.println(df4.format(12.34));   
运行结果: 
12.3 
12.3 
012.340 
12.34  

[java]  view plain  copy
  1. DecimalFormat format = new DecimalFormat("###,####.000");   
  2. System.out.println(format.format(111111123456.1227222));   
  3.   
  4. Locale.setDefault(Locale.US);   
  5. DecimalFormat usFormat = new DecimalFormat("###,###.000");   
  6. System.out.println(usFormat.format(111111123456.1227222));   
  7.   
  8. DecimalFormat addPattenFormat = new DecimalFormat();   
  9. addPattenFormat.applyPattern("##,###.000");   
  10. System.out.println(addPattenFormat.format(111111123456.1227));   
  11.   
  12. DecimalFormat zhiFormat = new DecimalFormat();   
  13. zhiFormat.applyPattern("0.000E0000");   
  14. System.out.println(zhiFormat.format(10000));   
  15. System.out.println(zhiFormat.format(12345678.345));   
  16.   
  17. DecimalFormat percentFormat = new DecimalFormat();   
  18. percentFormat.applyPattern("#0.000%");   
  19. System.out.println(percentFormat.format(0.3052222));   
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 "#,##,###,####" == "######,####" == "##,####,####".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325791801&siteId=291194637