DecimalFormat很强大
DecimalFormat的用法? Java 浮點(diǎn)數(shù) Float Double 小數(shù) 格式化 保留小數(shù)位后幾位等
?
?1?????????? DecimalFormat df = new DecimalFormat();
?2?????????? double data = 1234.56789;
?3?????????? System.out.println("格式化之前的數(shù)字: " + data);
?4?????????? String style = "0.0";//定義要顯示的數(shù)字的格式
?5?????????? df.applyPattern(style);// 將格式應(yīng)用于格式化器
?6?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
?7?????????? style = "00000.000 kg";//在格式后添加諸如單位等字符
?8?????????? df.applyPattern(style);
?9?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
10?????????? // 模式中的"#"表示如果該位存在字符,則顯示字符,如果不存在,則不顯示。
11?????????? style = "##000.000 kg";
12?????????? df.applyPattern(style);
13?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
14?????????? // 模式中的"-"表示輸出為負(fù)數(shù),要放在最前面
15?????????? style = "-000.000";
16?????????? df.applyPattern(style);
17?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
18?????????? // 模式中的","在數(shù)字中添加逗號(hào),方便讀數(shù)字
19?????????? style = "-0,000.0#";
20?????????? df.applyPattern(style);
21?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
22?????????? // 模式中的"E"表示輸出為指數(shù),"E"之前的字符串是底數(shù)的格式,
23?????????? // "E"之后的是字符串是指數(shù)的格式
24?????????? style = "0.00E000";
25?????????? df.applyPattern(style);
26?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
27?????????? // 模式中的"%"表示乘以100并顯示為百分?jǐn)?shù),要放在最后。
28?????????? style = "0.00%";
29?????????? df.applyPattern(style);
30?????????? System.out.println("采用style: " + style + "格式化之后: " + df.format(data));
31?????????? // 模式中的"\u2030"表示乘以1000并顯示為千分?jǐn)?shù),要放在最后。
32?????????? style = "0.00\u2030";
33?????????? //在構(gòu)造函數(shù)中設(shè)置數(shù)字格式
34?????????? DecimalFormat df1 = new DecimalFormat(style);
35?????????? //df.applyPattern(style);
36?????????? System.out.println("采用style: " + style + "格式化之后: " + df1.format(data));
?
格式化之前的數(shù)字: 1234.56789
采用style: 0.0格式化之后: 1234.6
采用style: 00000.000 kg格式化之后: 01234.568 kg
采用style: ##000.000 kg格式化之后: 1234.568 kg
采用style: -000.000格式化之后: -1234.568
采用style: -0,000.0#格式化之后: -1,234.57
采用style: 0.00E000格式化之后: 1.23E003
采用style: 0.00%格式化之后: 123456.79%
采用style: 0.00‰格式化之后: 1234567.89‰
轉(zhuǎn)載于:https://www.cnblogs.com/mingtian521/p/3689201.html
總結(jié)
以上是生活随笔為你收集整理的DecimalFormat很强大的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 学科竞赛管理系统服务器错误,学科竞赛管理
- 下一篇: Java 垃圾回收机制原理