java金钱千分位处理_java对金额的处理,保留两位小数、千分位符
關(guān)于數(shù)字特殊字符 0 和 #的一些簡單說明
#是一個(gè)特殊字符,用于數(shù)字部分(相當(dāng)于占位符);它表示顯示數(shù)字,但是最首位的0不予顯示。
data:02222.20
pattern:",###.##"
result:2,222.2
#用于整數(shù)部分沒有數(shù)位的限制,但是用于小數(shù)部分,卻有“最多只能有多少位小數(shù)的意思”,多余的進(jìn)行舍入。
data:2222.2222
pattern:",###.##"
result:2,222.22
0和#的用法是相近的,卻又略有不同;不同之處在于: 0限制整數(shù)部分最少出現(xiàn)的位數(shù);不足這個(gè)位數(shù)的,補(bǔ)0
data:22.26
pattern:"0,000.00"
result:0,022.26
0限制小數(shù)部分出現(xiàn)的位數(shù);不足補(bǔ)0,多的舍入
data:22.26
pattern:"00.000"
result:22.260
pattern:"00.0"
result:22.3
可以根據(jù)需求將#和0搭配使用:
//保留兩位小數(shù)
DecimalFormat df = new DecimalFormat("#0.00");
System.out.println(df.format(0.2));// 0.20
System.out.println(df.format(0.235));// 0.23
System.out.println(df.format(0.2351));// 0.24, 因?yàn)?.2351在0.23-0.24之間,距離0.24更近,所以輸出0.24
System.out.println(df.format(42));// 42.00
System.out.println(df.format(0.29));// 0.29
System.out.println(df.format(999999999));// 999999999.00
System.out.println(df.format(0));// 0.00
BigDecimal b=new BigDecimal("9999999999");//10位
System.out.println(df.format(b));//9999999999.00
b=new BigDecimal("9999999999999");//13位
System.out.println(df.format(b));//
System.out.println("----------------------");
//保留兩位小數(shù)并展示千分位符
DecimalFormat df1 = new DecimalFormat("##,##0.00");
System.out.println(df1.format(0.2));// 0.20
System.out.println(df1.format(0.235));// 0.24
System.out.println(df1.format(0.2351));// 0.24
System.out.println(df1.format(42));// 42.00
System.out.println(df1.format(0.29));// 0.29
System.out.println(df1.format(999999999));//999,999,999.00
System.out.println(df1.format(0));//0.00
b=new BigDecimal("9999999999");//10位 十億
System.out.println(df1.format(b));//9,999,999,999.00
b=new BigDecimal("9999999999999");//13位
System.out.println(df1.format(b));//9,999,999,999,999.00
參考:https://blog.csdn.net/pro_kang/article/details/72240921
總結(jié)
以上是生活随笔為你收集整理的java金钱千分位处理_java对金额的处理,保留两位小数、千分位符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cs结构航空订票系统java_VC++航
- 下一篇: java高效率素数算法_《Core Ja