BigDecimal保留小数处理

 

最近在处理支付相关的需求,涉及到金额的问题,采用传统的基本数据类型处理会存在误差,因此采用BigDecimal对象进行处理。

 

一、构造BigDecimal对象的方式

BigDecimal(int)       创建一个具有参数所指定整数值的对象。

BigDecimal(double) 创建一个具有参数所指定双精度值的对象。

BigDecimal(long)    创建一个具有参数所指定长整数值的对象。

BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。

注:建议采用BigDecimal(String)进行构造创建BigDecimal对象。

 

二、BigDecimal对象的加、减、乘、除操作

add(BigDecimal)        BigDecimal对象中的值相加,然后返回这个对象。

subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。

multiply(BigDecimal)  BigDecimal对象中的值相乘,然后返回这个对象。

divide(BigDecimal)     BigDecimal对象中的值相除,然后返回这个对象。

 

三、BigDecimal保留小数点问题

1、ROUND_DOWN 

Rounding mode to round towards zero. 

向零方向舍入 

示例:

 

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.55
2.52
1.61
1.11
1.01
-1.0-1
-1.1-1
-1.6-1
-2.5-2
-5.5-5

2、ROUND_UP 

Rounding mode to round away from zero. 

向远离0的方向舍入

示例:

输入数字使用 UP 舍入模式将输入数字舍入为一位数
5.56
2.53
1.62
1.12
1.01
-1.0-1
-1.1-2
-1.6-2
-2.5-3
-5.5-6


3、ROUND_CEILING 

Rounding mode to round towards positive infinity. 

向正无穷方向舍入 

示例:

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.56
2.53
1.62
1.12
1.01
-1.0-1
-1.1-1
-1.6-1
-2.5-2
-5.5-5

4、ROUND_FLOOR 

Rounding mode to round towards negative infinity. 

向负无穷方向舍入 

示例:

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.55
2.52
1.61
1.11
1.01
-1.0-1
-1.1-2
-1.6-2
-2.5-3
-5.5-6

5、ROUND_HALF_DOWN (相当于五舍六入

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. 

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5 

示例:

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.55
2.52
1.62
1.11
1.01
-1.0-1
-1.1-1
-1.6-2
-2.5-2
-5.5-5


6、ROUND_HALF_EVEN 

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. 

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN

解释:如果舍弃部分左边的数字为奇数,则舍入行为同 RoundingMode.HALF_UP;如果为偶数,则舍入行为同RoundingMode.HALF_DOWN。注意,在重复进行一系列计算时,根据统计学,此舍入模式可以在统计上将累加错误减到最小。此舍入模式也称为“银行家舍入法”,主要在美国使用。此舍入模式类似于 Java 中对float 和double 算法使用的舍入策略。

 示例:

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.56
2.52
1.62
1.11
1.01
-1.0-1
-1.1-1
-1.6-2
-2.5-2
-5.5-6



7、ROUND_HALF_UP (相当于四舍五入

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. 

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6 

 示例:

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.56
2.53
1.62
1.11
1.01
-1.0-1
-1.1-1
-1.6-2
-2.5-3
-5.5-6

8、ROUND_UNNECESSARY 

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. 

计算结果是精确的,不需要舍入模式 

解释:计算结果是精确的,不需要舍入,否则抛出 ArithmeticException。

输入数字使用 DOWN 舍入模式将输入数字舍入为一位数
5.5抛出 ArithmeticException
2.5抛出 ArithmeticException
1.6抛出 ArithmeticException
1.1抛出 ArithmeticException
1.01
-1.0-1
-1.1抛出 ArithmeticException
-1.6抛出 ArithmeticException
-2.5抛出 ArithmeticException
-5.5抛出 ArithmeticException

感谢道友的博客:https://my.oschina.net/sunchp/blog/670909 ,https://www.cnblogs.com/liqforstudy/p/5652517.html

转载于:https://www.cnblogs.com/limin12891/p/11116680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值