brief
The pattern of DecimalFormat contains positive and negative sub-patterns, such as "#,##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(-1221115151515151.541666); // Negative value BigDecimal bigDecimal2 = BigDecimal.valueOf(1221115151515151.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(bigDecimal2)); }}Output:
(12,211,151,515,151.54)12,211,151,515,151.54
Each sub-pattern consists of a prefix, a numerical part and a suffix. For example, the positive and negative pattern above can only have different prefixes and suffixes. The numerical part takes a positive pattern by default, which means that "#,##0.0#;(#)" is equivalent to "#,##0.0#;(#,##0.0#)" . ; The negative pattern afterward is optional, and can be none, if not, the negative value will be displayed in the default form (the prefix is "-" in most regions), such as -12,211,151,515,151.54 . Interestingly, for 0 values, the positive 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 parse strings directly:
System.out.print(decimalFormat.parse(",,,1,515,115.26262", new ParsePosition(0)));Output:
1515115.26262
As you can see, the decimalFormat.parse method is automatically removed . Previously , , it should be noted here that the first character of the parsed string must be a number, or followed by a number, otherwise an exception will be thrown or parsed as null . The second parameter of parse specifies the position of the first character to be parsed. The above example positions 0, 1, 2, 3 are all parsed from 1, and 4, 5 are all parsed from 5, that is, if taken , the bit is filled by the number next to it. If there are other characters except and numbers appear before parse , parse parse parse parse to the previous digit of this character, or if there are other characters except numbers appear after pares (including , ) then pares parse parse parse parse parse to the previous digit of this character.
If pattern contains multiple groups of characters with different numbers, for example: "#,##,###,####", then it uses the group next, 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 Counting Method
1234 can be represented as 1.234 x 10^3, and pattern is "0.###E0", which will format 1234 to 1.234E3.
Number of integers:
The number of significant numbers is derived from the sum of the minimum number of integer digits and the maximum number of decimal digits. For example, "##0.##E0" The minimum number of integer digits is 1 and the maximum number of decimal digits is 2, then the valid number is 3, and the formatted 12345 is "12.3E3". Except for the valid number, other items are omitted.
Numerical rounding rules
RoundingMode can be set through the method decimalFormat.setRoundingMode. The default is RoundingMode.HALF_EVEN.
It is out of sync. If accessed by multiple threads, you must implement synchronization by yourself
It is recommended to create a separate format instance for each thread. If multiple threads access a format at the same 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 to everyone's learning and I hope everyone will support Wulin.com more.