DecimalFormat 是 NumberFormat 的一個具體子類,用于格式化十進制數字。它可以支持不同類型的數,包括整數 (123)、定點數 (123.4)、科學記數法表示的數 (1.23E4)、百分數 (12%) 和金額 ($123)這些內容的本地化。
下邊先介紹下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
30
31
32
33
34
35
36
|
import java.text.*; import java.util.*; public class DecimalFormatDemo { public static void main(String args[]) { DecimalFormat df1 = new DecimalFormat( "###,###.0000" ); //使用系統默認的格式 System.out.println(df1.format( 111111123456.12 )); Locale.setDefault(Locale.US); DecimalFormat df2= new DecimalFormat( "###,###.0000" ); //使用美國的格式 System.out.println(df2.format( 111111123456.12 )); //----------------------------also use applypattern------------------------------// DecimalFormat df3= new DecimalFormat(); myformat3.applyPattern( "##,###.000" ); System.out.println(df3.format( 11112345.12345 )); //-----------------控制指數輸出-------------------------------------------------// DecimalFormat df4= new DecimalFormat(); myformat4.applyPattern( "0.000E0000" ); System.out.println(df4.format( 10000 )); System.out.println(df4.format( 12345678.345 )); //------------------百分數的輸出-------------------------------------------// DecimalFormat df5= null ; try { df5= (DecimalFormat)NumberFormat.getPercentInstance(); } catch (ClassCastException e){ <span style= "white-space:pre" > </span> System.err.println(e); } df5.applyPattern( "00.0000%" ); System.out.println(df5.format( 0.34567 )); System.out.println(df5.format( 1.34567 )); } } |
(1)對于數據的四舍五入:
DecimalFormat 包含一組符號,對于各符號的含義解釋如下:
0 一個數字
# 一個數字,不包括 0
. 小數的分隔符的占位符
, 分組分隔符的占位符
; 分隔格式。
- 缺省負數前綴。
% 乘以 100 和作為百分比顯示
? 乘以 1000 和作為千進制貨幣符顯示;用貨幣符號代替;如果雙寫,用國際貨幣符號代替。如果出現在一個模式中,用貨幣十進制分隔符代 替十進制分隔符。
X 前綴或后綴中使用的任何其它字符,用來引用前綴或后綴中的特殊字符。
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
DecimalFormat df1 = new DecimalFormat( "###0.00" ) ; //保留兩位小數,如果不足兩位小數則自動補零 System.out.println(df1.format( 124.367 )); System.out.println(df1.format( 124.3 )); DecimalFormat df2 = new DecimalFormat( "###0.##" ) ; //保留兩位小數,不足兩位則不補零 System.out.println(df2.format( 124.6 )); System.out.println(df2.format( 124 )); DecimalFormat df3 = new DecimalFormat( "000.000" ); //保留三位小數,哪里不足位則補零 System.out.println(df3.format( 24 )); DecimalFormat df = new DecimalFormat( "0.000E0000" ); //指數 System.out.println(df.format( 1234.56 )); DecimalFormat nf = (DecimalFormat)NumberFormat.getPercentInstance(); //百分數 System.out.println(nf.format( 0.476354 )); nf.applyPattern( "00.00%" ) ; System.out.println(nf.format( 0.476354 )); |
運行結果:
1
2
3
4
5
6
7
8
|
124.37 124.30 124.6 124 024.000 1 .235E0003 48 % 47.64 % |
(2)對于讀取并解析包含格式化的數字的字符串?解析支持包含在NumberFormat中。例如:
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.util.Locale; import java.text.NumberFormat; import java.text.ParseException; public class DecimalFormat5 { public static void main(String args[]) { // 本地格式 NumberFormat nf1 = NumberFormat.getInstance(); Object obj1 = null ; // 基于格式的解析 try { obj1 = nf1.parse( "1234,56" ); } catch (ParseException e1) { System.err.println(e1); } System.out.println(obj1); // 德國格式 NumberFormat nf2 =NumberFormat.getInstance(Locale.GERMAN); Object obj2 = null ; // 基于格式的解析 try { obj2 = nf2.parse( "1234,56" ); } catch (ParseException e2) { System.err.println(e2); } System.out.println(obj2); } } |
運行結果:
1
2
|
123456 //美國運行;被認為字符串 1234.56 //德國運行;被認為一個小數 |
(3)對于DecimalFormat和NumberFormat:
DecimalFormat是NumberFormat的一個子類,其實例被指定為特定的地區。因此,你可以使用NumberFormat.getInstance 指定一個地區,然后將結構強制轉換為一個DecimalFormat對象。文檔中提到這個技術可以在大多情況下適用,但是你需要用try/catch 塊包圍強制轉換以防轉換不能正常工作 (大概在非常不明顯得情況下使用一個奇異的地區)。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/u014704879/article/details/41479399