DecimalFormat的用法及各符號的意義
符號 | 位置 | 本地化? | 含義 |
0 | 數字 | 是 | 阿拉伯數字 |
# | 數字字 | 是 | 阿拉伯數字,如果不存在則顯示為空 |
. | 數字 | 是 | 小數分隔符或貨幣小數分隔符 |
- | 數字 | 是 | 減號 |
, | 數字 | 是 | 分組分隔符 |
E | 數字 | 是 | 分隔科學計數法中的尾數和指數。在前綴或后綴中無需加引號。 |
; | 子模式邊界 | 是 | 分隔正數和負數子模式 |
% | 前綴或后綴 | 是 | 乘以 100 并顯示為百分數 |
/u2030 | 前綴或后綴 | 是 | 乘以 1000 并顯示為千分數 |
¤(/u00A4) | 前綴或后綴 | 否 | 貨幣記號,由貨幣符號替換。如果兩個同時出現,則用國際貨幣符號替換。如果出現在某個模式中,則使用貨幣小數分隔符,而不使用小數分隔符。 |
' | 前綴或后綴 | 否 | 用于在前綴或或后綴中為特殊字符加引號,例如 "'#'#" 將 123 格式化為 "#123"。要創建單引號本身,請連續使用兩個單引號:"# o''clock"。 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
DecimalFormat format = new DecimalFormat( "###,####.000" ); System.out.println(format.format( 111111123456.1227222 )); // 1111,1112,3456.123 Locale.setDefault(Locale.US); DecimalFormat usFormat = new DecimalFormat( "###,###.000" ); System.out.println(usFormat.format( 111111123456.1227222 )); // 111,111,123,456.123 DecimalFormat addPattenFormat = new DecimalFormat(); addPattenFormat.applyPattern( "##,###.000" ); System.out.println(addPattenFormat.format( 111111123456.1227 )); // 111,111,123,456.123 DecimalFormat zhiFormat = new DecimalFormat(); zhiFormat.applyPattern( "0.000E0000" ); System.out.println(zhiFormat.format( 10000 )); // 1.000E0004 System.out.println(zhiFormat.format( 12345678.345 )); // 1.235E0007 DecimalFormat percentFormat = new DecimalFormat(); percentFormat.applyPattern( "#0.000%" ); System.out.println(percentFormat.format( 0.3052222 )); // 30.522% |
使用DecimalFormat時注意事項
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
|
import java.text.DecimalFormat; public class Format { public static void main(String[] args) { double ss= 12345.97468 ; double ss2= 0.23 ; DecimalFormat ff= new DecimalFormat( "#,###,###.######" ); 綠色區域為保留的小數位數(四舍五入)----但不會保 留末尾的 0 ) --------------------整數的話就沒有小數位 DecimalFormat ff2= new DecimalFormat( "#,###,###.0000" ); //小數點后保留四位小數(保留末尾的0) //類似于 round()函數---四舍五入 DecimalFormat ff3= new DecimalFormat( "#,###,###.00000" ); //小數點后保留五位小數(保留末尾的0) DecimalFormat ff4= new DecimalFormat( "#,###,###0.0000" ); //小數點后保留四位小數(保留末尾的0) DecimalFormat ff5= new DecimalFormat( "#,###,###0.00000" ); //小數點后保留五位小數(保留末尾的0) System.out.println( "#,###,###.######: " +ff.format(ss)); System.out.println( "#,###,###.0000 : " +ff2.format(ss)); //類似于round()函數---四舍五入 System.out.println( "#,###,###.00000 : " +ff3.format(ss)); System.out.println( "---------------------------------------------------" ); System.out.println( "f111#,###,###.######: " +ff.format(ss2)); System.out.println( "f222#,###,###.0000 : " +ff2.format(ss2)); System.out.println( "f333#,###,###.00000 : " +ff3.format(ss2)); System.out.println( "---------------------------------------------------" ); System.out.println( "f111#,###,###.######: " +ff.format(ss2)); System.out.println( "f444#,###,###.0000 : " +ff4.format(ss2)); System.out.println( "f555#,###,###.00000 : " +ff5.format(ss2)); System.out.println( "---------------------------------------------------" ); System.out.println( "sss#,###,###.0000 : " +ff4.format(ss)); System.out.println( "sss#,###,###.00000 : " +ff5.format(ss)); } } |
結果:
#,###,###.######: 12,345.97468
#,###,###.0000 : 12,345.9747
#,###,###.00000 : 12,345.97468
---------------------------------------------------
f111#,###,###.######: 0.23
f222#,###,###.0000 : .2300 (有問題)
f333#,###,###.00000 : .23000 (有問題)
---------------------------------------------------
f111#,###,###.######: 0.23
f444#,###,###.0000 : 0.2300
f555#,###,###.00000 : 0.23000---------------------------------------------------
sss#,###,###.0000 : 1,2345.9747 (有問題) ---千位符位置有問題
sss#,###,###.00000 : 1,2345.97468 (有問題) ---千位符位置有問題
總結一下吧
(1)double ss2=0.23;時使用:
1
|
DecimalFormat ff4= new DecimalFormat( "#,###,###0.0000" ); //小數點后保留四位小數(保留末尾的0) |
(2)double ss=12345.97468;時使用:
1
|
DecimalFormat ff4= new DecimalFormat( "#,###,###.0000" ); //小數點后保留四位小數(保留末尾的0) |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_28114159/article/details/105862909