BigDecimal不整除的一个异常java.lang.ArithmeticException
转载地址:http://blog.csdn.net/jobjava/article/details/6764623
金额的数据类型是BigDecimal
通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常的,异常如下:Java.lang.ArithmeticException:
Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(Unknown Source)
应用场景:一批中供客户的单价是1000元/年,如果按月计算的话1000/12=83.3333333333....
解决之道:就是给divide设置精确的小数点divide(xxxxx,2, BigDecimal.ROUND_HALF_EVEN)
在 java中, 四舍五入通过 BigDecimal 来实现。一定要注意:BigDecimal is Immutable。
也就是跟String一样,对前一个的修改,比如setScale(), add()等都会返回一个新的BigDecimal.
四舍五入舍入模式是 BigDecimal.ROUND_HALF_UP
BigDecimal定义了一下舍入模式,只有在作除法运算或四舍五入时才用到舍入模式,
下面简单介绍,详细请查阅J2se API文档
|
static int |
Rounding mode to round towards zero. 向零方向舍入 |
|
static int |
Rounding mode to round towards negative infinity. 向负无穷方向舍入 |
|
static int |
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入,例如1.55保留一位 小数结果为1.5 |
|
static int |
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 |
|
static int |
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位 小数结果为1.6 |
|
static int |
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. 计算结果是精确的,不需要舍入模式 |
|
static int |
Rounding mode to round away from zero. 向远离0的方向舍入 |
BigDecimal不整除的一个异常java.lang.ArithmeticException的更多相关文章
- 【异常】Phoenix异常:java.lang.ArithmeticException: Rounding necessary
1 异常sql upsert into WMBIGDATA.ODS_ES_CHARGING_STATION(id,evcosType,address,serviceTel,supportOrder,o ...
- 除法运算时的一个常见异常之java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
一.背景 今天在计算库存消耗百分比(消耗的库存/总库存)的时候遇到了一个错误,java.lang.ArithmeticException: Non-terminating decimal expans ...
- BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result的解决办法
BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represent ...
- java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法
今天在写一个JAVA程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact repr ...
- maven(多个模块)项目 部署 开发环境 问题处理历程【异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE】
maven(多个模块)项目 部署 开发环境 问题处理历程[异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE] 201 ...
- 项目启动异常,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo ...
- 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method
异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...
- 【已解决】报异常:java.lang.ArithmeticException: / by zero
异常名称如下:java.lang.ArithmeticException: / by zero 原因:当我们定义的被除数为整形时候(short int long)会抛出此异常,被除数为整形时不可以 ...
- 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity
异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...
随机推荐
- VUE获取焦点
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- E - Apple Tree POJ - 2486
E - Apple Tree POJ - 2486 Wshxzt is a lovely girl. She likes apple very much. One day HX takes her t ...
- Res-net 标准版本源码差异-官方源码示例
# resnet https://github.com/tensorflow/models/blob/master/research/slim/nets/resnet_v1.py https://gi ...
- Pythia:Facebook最新开源的视觉、语言多任务学习框架
Facebook 发布了一个全新的多任务学习框架 Pythia,它基于 PyTorch 且可用于视觉和语言的联合任务.Pythia 是一种模块化的即插即用框架,数据科学家和机器学习开发者能快速构建.复 ...
- redis(2)
目 录 1内容 3 2 redis集群简介 3 2.1 集群的概念 3 2.1.1 使用redis集群的必要性 3 2.1.2 如何学习redis集群 3 3 redis主从复制 4 3.1 概 ...
- Python重学记录2
这几天学的不多,只是看了一下相关的视频.最近看的部分比较难,装饰器没有搞懂,__slots__和property也不太明白(这两个知识点是在公交车上看的视频,因为1.5倍速度放的视频,看得快,不太明白 ...
- poj2243前一道题升级(思维构造+ac自动机)
题:http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意:给出m个模式串,求长度小于n的且存在模式串的字符串数有多少个(a~z) 分析:我们反着来,用总的 ...
- geodjango七日学习笔记 (7.30整理本地笔记上传到网络)
第一天进行到现在,在开端的尾巴,想起来写一个学习笔记, 开发环境已搭好,用的是pycharm 环境是本机已有的interpreter python3.7 接下来要做的是新建一个geodjango项 ...
- 02-python-运算符与表达式
目录 1. 比较运算符 2. 算数运算符 3. 赋值运算符 4. 位于运算符 5. 逻辑运算符 6. 成员运算符 7. 身份运算符 8. 运算符优先级 9. 输出输入 10. 数字类型转换及常用数学方 ...
- SpringBoot2中,怎么生成静态文档
SpringBoot2中,怎么生成静态文档 在实际开发过程中,我们通过swagger就可以生成我们的接口文档,这个文档就可以提供给前端人员开发使用的.但是,有时候,我们需要把我们的接口文档,提供给第三 ...