首页 > 其他 > 详细

double类型计算精度确实问题

时间:2017-04-01 22:05:18      阅读:291      评论:0      收藏:0      [点我收藏+]

1.BigDecimal传入double类型参数精度丢失:

例子:

(0.47+0.36)/2 =0.42  !=  (0.46999+0.35999)/2=0.414995(使用bigdecial保留两位小数为0.41)

 

技术分享

技术分享

解决方案:

  1. BigDecimal溢出:传入String类型参数

两个构造方法:a.BigDecimal(double val)  Translates a double into a BigDecimal. 

注意:Note: the results of this constructor can be somewhat unpredictable.
b.BigDecimal(String val) Translates the String repre sentation of a BigDecimal into a BigDecimal.

技术分享

技术分享

2.      double类型运算精度丢失

例子( 0.48+0.57)/2=0.53  !=  1.0499999999999998/2=0.5249999999999999=0.52

 

解决方案:

使用BigDecimal进行运算

 技术分享

技术分享

double类型计算精度确实问题

原文:http://leidai.blog.51cto.com/9661375/1912459

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!