簡要
DecimalFormat 的 pattern 都包含著 正負子 pattern ,例如 “#,##0.00;(#,##0.00)”:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * Created by Shuai on 2016/7/11. */ public class Main { public static void main(String[] args) { // 正值 BigDecimal bigDecimal = BigDecimal.valueOf(- 12211151515151.541666 ); // 負值 BigDecimal bigDecimal2 = 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(bigDecimal2)); } } |
輸出:
1
2
|
( 12 , 211 , 151 , 515 , 151.54 ) 12 , 211 , 151 , 515 , 151.54 |
每一個子 pattern 都由前綴,數值部分和后綴組成,像上面的正負 pattern 只能是前綴和后綴不同, 數值部分默認取正 pattern 的,這就意味著 "#,##0.0#;(#)" 就等同與 "#,##0.0#;(#,##0.0#)" 。;后面的負pattern是可選的,可以沒有,如果沒有,負值會以默認的形式顯示(在大多數地區前綴是“-”),例如 -12,211,151,515,151.54。有趣的是 對于 0 值,都會取正 pattern:
1
2
3
4
5
6
7
8
9
10
11
12
|
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)); } } |
輸出:
1
2
|
0.00 0.00 |
DecimalFormat 可以直接解析字符串:
1
|
System.out.print(decimalFormat.parse( ",,,1,515,115.26262" , new ParsePosition( 0 ))); |
輸出:
1
|
1515115.26262 |
可以看到,decimalFormat.parse 方法都自動去掉了.之前的,,這里要注意的是,解析的字符串第一個字符必須是數字,或者,后緊跟著數字,否則會拋出異常或者解析為null。parse 的第二個參數指定了解析的第一個字符的位置,上面的例子 位置 0,1,2,3 都是從1開始解析,4,5都是從5開始解析,即如果取,位則由后面緊挨著的數字補位。如果.前面出現了除,和數字外其他的字符則parse解析到這個字符的前一位,或者.后面出現了除數字外的其他字符(包括, )則pares 解析到這個字符的前一位。
如果 pattern 包含多組個數不同的字符,例如:"#,##,###,####", 那它使用的是組后一組,即"#,##,###,####" == "######,####" == "##,####,####" :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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)); } } |
輸出:
1
2
3
|
656 , 5232 , 3265.63 656 , 5232 , 3265.63 656 , 5232 , 3265.63 |
Special Pattern Characters
科學計數法
1234 可以表示為 1.234 x 10^3,pattern 為 “0.###E0”,就會把 1234 格式化為 1.234E3。
整數的個數:
- 如果整數位的最大個數大于最小個數而且大于1,就會強制指數是整數位最大個數的倍數,整數位最小個數視為1。例如:”##0.#####E0”, 整數為最大個數是3,最小個數是1,則指數必須是3的倍數,而且,最小要有1位整數。12345 格式化為 “12.345E3”, 123456 格式化為 “123.456E3”, 123 格式化為 “123E0”(整數位必須至少有1位,且不能是0,指數為3的倍數)。
- 否則,由整數的最小個數來調整指數,”00.###E0” 格式化 0.00123 為”12.3E-4”。
有效數字個數由整數位的最小個數與小數位的最大個數之和得出,例如 “##0.##E0” 整數位最小個數為1,小數位最大個數為2,則有效個數是3,格式化 12345 為 “12.3E3”。除有效個數外,其他省略。
數值舍入規則
可以通過方法 decimalFormat.setRoundingMode 來設置 RoundingMode,默認使用的是RoundingMode.HALF_EVEN.
它不同步,如果多線程訪問,要自己實現同步
建議為每個線程創建單獨的格式實例。如果多個線程同時訪問一個格式,它必須在外部同步。
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// 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) {} } } |
參考:原文地址
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/yang786654260/article/details/51883338