关于BigDecimal 和 double 类型保存金钱,以及精度问题,银行家舍入法
1、 BigDecimal 类型数据 的创建,构造函数 有
public BigDecimal(BigInteger intVal, long val, int scale, int prec); public BigDecimal(char[] in, int offset, int len) ; public BigDecimal(String val); public BigDecimal(double val); public BigDecimal(BigInteger val); public BigDecimal(int val); public BigDecimal(long val);
其中 构造方法
public BigDecimal(double val)
可能是不可预测的,因为double类型数据 有精度问题, 比如 double a = 0.1, new BigDecimal(a); 构造出来的BigDecimal 是不等于 0.1的, 解决方法是 将其转为String 或者用 。
double a = 5.5D;
double b = 4.5D;
double c = 1.465D;
double d = 1.455D;
BigDecimal decimal = new BigDecimal(c).setScale(2, RoundingMode.HALF_EVEN); //构造的BigDecimal 不等于 1.465, 不精确 :1.4650000000000000799360577730112709105014801025390625
BigDecimal decimal2 = BigDecimal.valueOf(c).setScale(2, RoundingMode.HALF_EVEN); //这样构造的BigDecimal 才精确
BigDecimal decimal3 = new BigDecimal(c+"").setScale(2, RoundingMode.HALF_EVEN); //这样构造的BigDecimal 才精确 System.out.println(new BigDecimal(a).setScale(0, RoundingMode.HALF_EVEN).doubleValue());
System.out.println(new BigDecimal(b).setScale(0, RoundingMode.HALF_EVEN).doubleValue());
System.out.println(new BigDecimal(c+"").setScale(2, RoundingMode.HALF_EVEN).doubleValue());
System.out.println(new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
2、 BigDecimal 跟String 对象一样是不可变对象, 设置保留小数位数后 返回的是一个新的对象。
3、BigDecimal 舍入模式模式有 ROUNDING_UP 远离0, ROUNDING_DOWN 接近0, ROUNDING_CEILING 向上进位, ROUNDING_FLOOR 向下靠近, ROUNDING_HALF_UP 四舍五入, ROUNDING_HALF_DOWN 五舍六入, ROUNDING_HALF_EVEN 银行家舍入法,四舍六入,五后非零入,五后为零靠前位奇偶,前偶舍前奇入
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
public final static int ROUND_UP = 0; /**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
public final static int ROUND_DOWN = 1; /**
* Rounding mode to round towards positive infinity. If the
* {@code BigDecimal} is positive, behaves as for
* {@code ROUND_UP}; if negative, behaves as for
* {@code ROUND_DOWN}. Note that this rounding mode never
* decreases the calculated value.
*/
public final static int ROUND_CEILING = 2; /**
* Rounding mode to round towards negative infinity. If the
* {@code BigDecimal} is positive, behave as for
* {@code ROUND_DOWN}; if negative, behave as for
* {@code ROUND_UP}. Note that this rounding mode never
* increases the calculated value.
*/
public final static int ROUND_FLOOR = 3; /**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4; /**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal >} 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
public final static int ROUND_HALF_DOWN = 5; /**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
public final static int ROUND_HALF_EVEN = 6; /**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*/
public final static int ROUND_UNNECESSARY = 7;
4、 double 类型数据保存到 数据库的 decimal 类型字段时, mybatis是采用了 四舍五入的 舍入模式。
参考资料:
1、提高你的Java代码质量吧:不容忽视的四舍五入细节 http://blog.csdn.net/p106786860/article/details/9377471
关于BigDecimal 和 double 类型保存金钱,以及精度问题,银行家舍入法的更多相关文章
- 两个Double类型相减出现精度丢失问题
两个Double类型相减出现精度丢失问题 720.50-279.5=440.099999999 而不是440.1 解决方法,将数据库中的类型改为decimal类型,小数精确到2位
- java中如何使用BigDecimal使得Double类型保留两位有效数字
一.场景:从数据表中读出Decimal类型的数据直接塞给Double类型的对象时,并不会有什么异常. 如果要再此基础上计算,就会发生异常. 比如:读出数据为0.0092,将其乘以100,则变成了0.9 ...
- Double 类型运算时的精度问题
double 类型运算时的 计算的精度不高,常常会出现0.999999999999999这种情况,那么就须要用BigDecimal 它是java提供的用来高精度计算的工具类 以下是对这个类的一个包 ...
- 在使用hibernate注解的时候,想对double类型的字段进行精度约束
@Column(name = "price",precision = 10,scale = 2) public double getPrice() { return price; ...
- double 类型运算会出现精度问题
要先转换为字符串,后进行运算,可以写个方法做乘法运算public static double mul(double v1,double v2){BigDecimal b1 = new BigDecim ...
- BigDecimal初始化不要用double类型
在进行单价.总价相关的计算时,就会用到BigDecimal. 在初始化时,一个不小心,就可能给自己挖坑. 示例如下: public class BigDecimalInitTest { public ...
- Java:利用BigDecimal类巧妙处理Double类型精度丢失
目录 本篇要点 经典问题:浮点数精度丢失 十进制整数如何转化为二进制整数? 十进制小数如何转化为二进制数? 如何用BigDecimal解决double精度问题? new BigDecimal(doub ...
- MySQL中Decimal类型和Float Double的区别 & BigDecimal与Double使用场景
MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,double等非标准类型,在DB中保存的是近似值,而Decimal则以字符串的形 ...
- Java 中浮点数---------BigDecimal和double(初探)
为什么要使用 bigdecimal? 借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了 ...
随机推荐
- [WPF系列]-参考文献
DataTemplates Data Templating Overview Styling and Templating DataTemplate Class FrameworkTemplate ...
- 一个开关电源传导、辐射处理案例-----Layout环路
这是一款输入宽电压120-277V 60HZ,输出48V,273mA的电源,使用美芯晟MT7933,采用Buck拓扑结构. 注:在最初的设计中,预留电感L1.L2,CBB电容C1.C2作为传导测试元 ...
- 【2016-10-31】【坚持学习】【Day16】【MongoDB】【入门 -概念】
MongoDB 概念解析 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是文档.集合.数据库,下面我们挨个介绍. 下表将帮助您更容易理解Mongo中的一些概念: SQL术 ...
- [No000080]右键解锁增强Chrome插件开发,破除防复制
昨天用360极速(虽然我不喜欢360.)浏览器,登陆知乎查阅一些东西,突然感觉有些观点很赞同,想copy转载一下,我了个去,它丫的居然不让我复制. 地址:https://www.zhihu.com/q ...
- 面试中关于Java你所需知道的的一切
本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺. 1. Java中的原始数据类型都有哪些, ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 区域管理功能增强(电子商务方向)
由于公司是面向全国服务的.全国各地都有分公司,需要管理到覆盖全国的各种业务,各种业务系统信息系统的数据都需要规范化. 公司开展网络订单功能,在全国范围内实现网络下单.提高工作效率,提高各公司之间的数据 ...
- ToolProvider.getSystemJavaCompiler() Return NULL!
http://www.cnblogs.com/fangwenyu/archive/2011/10/12/2209051.html
- Struts2:Json插件_Ajax
lib中加入包 struts2-json-plugin-2.3.20.jar json插件有自己的过滤器.返回类型 WebRoot下新建js文件夹 放入json2.js json2.js是一个著名开源 ...
- YII2如何修改默认控制器/方法
在网上找了非常多的方法,但是都不好使最后自己综合网上所有自己琢磨出来的,见笑了 首先Yii2中在/vendor/yiisoft/yii2/web/Application.php的第28行 public ...
- rpc框架之 thrift连接池实现
接前一篇rpc框架之HA/负载均衡构架设计 继续,写了一个简单的thrift 连接池: 先做点准备工作: package yjmyzz; public class ServerInfo { publi ...