Double转换大数字出现精度丢失或者错误,如18位或以上的数字超过100000000000000000打印出来就会出现错误,用BigDecimal或者BigInteger能解决问题,Double的最大值也远远超过这个了呀,想知道为啥Double类型为什么会这种情况? NumberFormat nf = NumberFormat.getInstance(); nf.setGroupingUsed(false);
System.out.println(nf.format(Double.valueOf("100000000000000000")));//100000000000000000
System.out.println(nf.format(Double.valueOf("100000000000000001")));//100000000000000000
System.out.println(nf.format(Double.parseDouble("100000000000000216")));//100000000000000224
System.out.println(new BigDecimal("100000000000000216"));//100000000000000216
添加回答
举报
0/150
提交
取消