java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法
今天在写一个JAVA程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result。
发现报错的语句是:
|
1
|
foo.divide(bar)); |
原来JAVA中如果用BigDecimal做除法的时候一定要在divide方法中传递第二个参数,定义精确到小数点后几位,否则在不整除的情况下,结果是无限循环小数时,就会抛出以上异常。
解决方法:
|
1
|
foo.divide(bar, 2, BigDecimal.ROUND_HALF_UP); |
注意这个divide方法有两个重载的方法,一个是传两个参数的,一个是传三个参数的:
两个参数的方法:
@param divisor value by which this {@code BigDecimal} is to be divided. 传入除数
@param roundingMode rounding mode to apply. 传入round的模式
三个参数的方法:
@param divisor value by which this {@code BigDecimal} is to be divided. 传入除数
@param scale scale of the {@code BigDecimal} quotient to be returned. 传入精度
@param roundingMode rounding mode to apply. 传入round的模式
Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/2327.html
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法的更多相关文章
- 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.lang.ArithmeticException: Non-terminating decimal expans ...
- Bigdecimal: Non-terminating decimal expansion; no exact representable decimal result.
做除法没有指定保留小数点后几位,就会抛出此异常. 因为会除不尽 Non-terminating decimal expansion; no exact representable decimal re ...
- Non-terminating decimal expansion; no exact representable decimal result.
Non-terminating decimal expansion; no exact representable decimal result. 翻译为:非终止十进制扩展; 没有确切的可表示的小数 ...
- 【异常】java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
异常原因:没有指定数据精度导致数据运算无法正常结算 如执行下面的除法: ).divide(BigDecimal.valueOf()).intValue(); 指定精度后就可以了: ).divide(B ...
- 【java错误】Non-terminating decimal expansion; no exact representable decimal result
问题描述 意思是“无法结束的除法表达式:没有精确的除结果”.当时输入的10/3,结果应该是3.3333....333. 下面这种处理方式有问题. BigDecimal num3 = new BigDe ...
- Java_类似java.lang.VerifyError: Expecting a stackmap frame at branch target 22 in method的解决方法
报异常的方法内使用了Java 7的新特性:自动资源释放,类似于try(){},即在try后面跟一括号,在括号里面对一些资源赋值,try里面的代码块执行完毕之后会自动释放try后面的括号中声明的资源. ...
- BigDecimal不整除的一个异常java.lang.ArithmeticException
转载地址:http://blog.csdn.net/jobjava/article/details/6764623 金额的数据类型是BigDecimal 通过BigDecimal的divide方法进行 ...
- java.lang.ArithmeticException: / by zero
随机推荐
- 拖图UI和纯代码UI
1拖图UI, 优点:适合快速实验各种天马行空的想法 缺点:太多的storyBoard不好管理,不适合较大的项目,如果一个项目有价值,或成熟了,为了维护拓展,就最好改为纯代码 2纯代码UI 优点:1好维 ...
- Elasticsearch-基础介绍及索引原理分析
介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 L ...
- canvas学习之小球动画
项目地址:http://pan.baidu.com/s/1skZGPgL 最近学习使用canvas做动画效果,主要原理就是创建一个小球对象,然后小球对象有一个moveball方法,每次让小球沿着随机路 ...
- mac使用迁移助理迁移数据之后homestead无法打开
1. 错误大致如下: here was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling Vir ...
- git clone项目
1. 生成公钥和私钥 ssh-keygen 2. 将公钥添加到github或者gitlab上,一般github或者gitlab允许添加多个公钥,可能是考虑到用户使用不同的机器了吧,还是很贴心的. 3. ...
- spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...
- QQ企业邮箱接口
我推荐的这篇文章很好:http://wenku.baidu.com/link?url=KQIMyrECGb8GS_0fag4PRG64M8Z7wOLbsU1f3BhydXAyYJDC2JMHEuVcy ...
- CF-503div2-A/B/C
A. New Building for SIS time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CO15批次确定,标准的太蛋疼了
1.批次确定的配置,之前有转过,自己也动手配过,可以是可以,但是蛋疼,用户不愿意去弹出的界面选批次...2.因为这边的批次全部是按年月日+流水生成的,所以在批次确定这里就需要按照批次的号来排序选择了 ...
- zk 创建瞬时、顺序节点的原理
命令: create -s -e /worker/lock xx zk 的实现代码在:PrepRequestProcessor.pRequest2Txn 中 //The number of chang ...