文章目录
- 1. 背景
- 2. 方法
- 3. 总结
1. 背景
昨天和小伙伴排查一个问题时,发现一个 BigDecimal 精度丢失的问题,即
double a = 1.1;
BigDecimal ba = new BigDecimal(a).subtract(new BigDecimal(0.1));
System.out.println(ba);
输出:
1.0000000000000000832667268468867405317723751068115234375
随后测试了其他类型转为 BigDecimal 后参与计算的效果。
int c = 1;
BigDecimal cb = new BigDecimal(c).subtract(new BigDecimal(0.1));
System.out.println("int---" + cb);
Integer ci = 1;
BigDecimal cbi = new BigDecimal(ci).subtract(new BigDecimal(0.1));
System.out.println("Integer---" + cbi);
float a = 1.1f;
BigDecimal fb = new BigDecimal(a).subtract(new BigDecimal(0.1));
System.out.println("float---" + fb);
Float fa = 1.1F;
BigDecimal fba = new BigDecimal(fa).subtract(new BigDecimal(0.1));
System.out.println("Float---" + fba);
double b = 1.1d;
BigDecimal bb = new BigDecimal(b).subtract(new BigDecimal(0.1));
System.out.println("double---" + bb);
Double db = 1.1D;
BigDecimal dbb = new BigDecimal(db).subtract(new BigDecimal(0.1));
System.out.println("Double---" + dbb);
String str = "1.1";
BigDecimal sb = new BigDecimal(str).subtract(new BigDecimal(0.1));
System.out.println("String---" + sb);
BigDecimal bd = new BigDecimal("1.1").subtract(new BigDecimal(0.1));
System.out.println("BigDecimal---" + bd);
输出:
Integer---0.8999999999999999944488848768742172978818416595458984375
float---1.0000000238418579046051348768742172978818416595458984375
Float---1.0000000238418579046051348768742172978818416595458984375
double---1.0000000000000000832667268468867405317723751068115234375
Double---1.0000000000000000832667268468867405317723751068115234375
String---0.9999999999999999944488848768742172978818416595458984375
BigDecimal---0.9999999999999999944488848768742172978818416595458984375
效果都是不行的。
2. 方法
以下几种方式都可以:
BigDecimal bd2 = new BigDecimal(String.valueOf("1.1")).subtract(new BigDecimal("0.1"));
System.out.println("String.valueOf---" + bd2);
BigDecimal bd3 = new BigDecimal(Double.toString(1.1d)).subtract(new BigDecimal("0.1"));
System.out.println("Double.toString---" + bd3);
BigDecimal bv = BigDecimal.valueOf(1.1d).subtract(BigDecimal.valueOf(0.1));
System.out.println("BigDecimal.valueOf---" + bv);
输出:
String.valueOf---1.0
Double.toString---1.0
BigDecimal.valueOf---1.0
需要注意的是,new BigDecimal(str) 会有问题,
new BigDecimal(String.valueOf(str)) 却不会。
3. 总结
推荐使用
new BigDecimal(String.valueOf("1.1"))
new BigDecimal(Double.toString(1.1d))
BigDecimal.valueOf(1.1d)
(图网,侵删)