Java. text. DecimalFormat usage details,
Brief
The pattern of DecimalFormat contains the plus and minus child pattern, for example, "#, #0.00; (#, #0.00 )":
/*** Created by Shuai on 2016/7/11. */public class Main {public static void main (String [] args) {// positive value BigDecimal bigDecimal = BigDecimal. valueOf (-12211151515151.541666); // negative value BigDecimal = BigDecimal. valueOf (12211151515151.541666); String pattern = "#,## 0.00; (#,## 0.00)"; DecimalFormat decimalFormat = new DecimalFormat (pattern); decimalFormat. format (bigDecimal); System. out. println (decimalFormat. format (bigDecimal); System. out. print (decimalFormat. format (bigdecimal ));}}
Output:
(12,211,151,515,151.54)12,211,151,515,151.54
Each child pattern consists of a prefix, a numerical part, and a suffix. For example, the positive and negative pattern above can only be a different prefix and a suffix, and the numerical part is a positive pattern by default, which means"#,## 0.0 #;(#)"It is equivalent"#,## 0.0 #; (#,## 0.0 #)".;Negative pattern is optional and optional. If not, negative values are displayed in the default format (in most regions, the prefix is "-"). For example:-12,211,151,515,151.54. It is interesting that for 0 values, the regular pattern will be taken:
public class Main { public static void main(String[] args) { BigDecimal bigDecimal = BigDecimal.valueOf(-0.00); BigDecimal bigDecimal2 = BigDecimal.valueOf(0.00); String pattern = "0.00;(0.00)"; DecimalFormat decimalFormat = new DecimalFormat(pattern); decimalFormat.format(bigDecimal); System.out.println(decimalFormat.format(bigDecimal)); System.out.print(decimalFormat.format(bigDecimal2)); }}
Output:
0.000.00
DecimalFormat can directly parse strings:
System.out.print(decimalFormat.parse(",,,1,515,115.26262", new ParsePosition(0)));
Output:
1515115.26262
We can see that the decimalFormat. parse method is removed automatically..Previous,The first character of the parsed string must be a number or followed by a number. Otherwise, an exception is thrown orNull. The second parameter of parse specifies the position of the first character to be parsed. In the preceding example, the positions 0, 1, 2, and 3 are parsed from 1, and the positions 4 and 5 are parsed from 5.,Bit is supplemented by the number next to it. If.Division,Parse is resolved to the first digit of the character, or.Other characters except numbers (including,), Pares is parsed to the first digit of the character.
If pattern contains multiple groups of different characters, for example:"#,##,###,####",It uses the last group, that is"#,###########" = "##############" = "##, ####,####":
public class Main { public static void main(String[] args) { BigDecimal bigDecimal = BigDecimal.valueOf(65652323265.626262); String pattern = "#,##,###,###0.00"; String pattern2 = "######,###0.00"; String pattern3 = "##,####,###0.00"; DecimalFormat decimalFormat = new DecimalFormat(pattern); System.out.println(decimalFormat.format(bigDecimal)); decimalFormat.applyPattern(pattern2); System.out.println(decimalFormat.format(bigDecimal)); decimalFormat.applyPattern(pattern3); System.out.println(decimalFormat.format(bigDecimal)); }}
Output:
656,5232,3265.63656,5232,3265.63656,5232,3265.63
Special Pattern Characters
Scientific notation
1234 can be expressed as 1.234x10 ^ 3, and pattern is "0. ### E0", and 1234 is formatted as 1.234E3.
Number of integers:
- If the maximum number of integers is greater than the minimum number and greater than 1, the index is a multiple of the maximum number of integers. The minimum number of integers is regarded as 1. Example: "#0. ##### E0 ", where the integer is 3 and the minimum number is 1, the index must be a multiple of 3, and the minimum value must be 1 integer. 12345 format to "12.345E3", 123456 format to "123.456E3", 123 format to "123E0" (the integer must have at least one digit and cannot be 0, returns a multiple of 3 ).
- Otherwise, the minimum number of integers is used to adjust the index. "00. ### E0" is formatted as "0.00123-4 ".
The number of valid digits is derived from the sum of the minimum number of digits and the maximum number of decimal places, for example, "#0. # E0 "the minimum number of digits is 1 and the maximum number of decimal places is 2. The valid number is 3 and the format 12345 is" 12.3E3 ". This parameter is omitted except the number of valid values.
Value Rounding Rule
You can set the RoundingMode by decimalFormat. setRoundingMode. The RoundingMode. HALF_EVEN is used by default.
It is not synchronized. If multi-thread access is required, you must implement synchronization on your own.
We recommend that you create a separate format instance for each thread. If multiple threads access one format at a time, it must be synchronized externally.
Example
// Print out a number using the localized number, integer, currency, // and percent format for each locale Locale[] locales = NumberFormat.getAvailableLocales(); double myNumber = -1234.56; NumberFormat form; for (int j=0; j<4; ++j) { System.out.println("FORMAT"); for (int i = 0; i < locales.length; ++i) { if (locales[i].getCountry().length() == 0) { continue; // Skip language-only locales } System.out.print(locales[i].getDisplayName()); switch (j) { case 0: form = NumberFormat.getInstance(locales[i]); break; case 1: form = NumberFormat.getIntegerInstance(locales[i]); break; case 2: form = NumberFormat.getCurrencyInstance(locales[i]); break; default: form = NumberFormat.getPercentInstance(locales[i]); break; } if (form instanceof DecimalFormat) { System.out.print(": " + ((DecimalFormat) form).toPattern()); } System.out.print(" -> " + form.format(myNumber)); try { System.out.println(" -> " + form.parse(form.format(myNumber))); } catch (ParseException e) {} } }
Reference: original address
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.