Non-terminating decimal expansion; no exact representable decimal result.
Non-terminating decimal expansion; no exact representable decimal result. 翻译为:非终止十进制扩展; 没有确切的可表示的小数结果;
翻译为人话就是说:BigDecimal是为高精度计算而设计的,而你的值是没有精确结果的;
举例:
BigDecimal b1=new BigDecimal(1.0);
BigDecimal b2=new BigDecimal(3.0);
BigDecimal c=b1.divide(b2);
1.0/3.0是除不尽的,所以抛出异常
解决方案:使用BigDecimal.divide(BigDecimal divisor, int scale, RoundingMode roundingMode) ;
BigDecimal b1=new BigDecimal(1.0);
BigDecimal b2=new BigDecimal(3.0);
BigDecimal c=b1.divide(b2,3,RoundingMode.HALF_UP);
其中 scale 为保留的小数位;RoundingMode roundingMode 为取舍模式(如:RoundingMode.HALF_UP 四舍五入);
RoundingMode具有哪些模式请参照我的另一篇博客:https://www.cnblogs.com/minxl/p/10247770.html
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程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact repr ...
- Bigdecimal: Non-terminating decimal expansion; no exact representable decimal result.
做除法没有指定保留小数点后几位,就会抛出此异常. 因为会除不尽 Non-terminating decimal expansion; no exact representable decimal re ...
- 除法运算时的一个常见异常之java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
一.背景 今天在计算库存消耗百分比(消耗的库存/总库存)的时候遇到了一个错误,java.lang.ArithmeticException: Non-terminating decimal expans ...
- 【java错误】Non-terminating decimal expansion; no exact representable decimal result
问题描述 意思是“无法结束的除法表达式:没有精确的除结果”.当时输入的10/3,结果应该是3.3333....333. 下面这种处理方式有问题. BigDecimal num3 = new BigDe ...
- 【异常】java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
异常原因:没有指定数据精度导致数据运算无法正常结算 如执行下面的除法: ).divide(BigDecimal.valueOf()).intValue(); 指定精度后就可以了: ).divide(B ...
- decimal.ToString("#0.00")与decimal.ToString("#.##")的区别
decimal decTemp = 2.1m; Console.WriteLine(decTemp.ToString("#0.00")); //输出2.10 Console.Wri ...
- iOS Round Double value to 2 decimal digits and round 3rd decimal digit up
I have I double value like this one 17.125. It should be rounded up to 17.13 If I use the simple met ...
- Java-Class-C:java.util.BigDecimal
ylbtech-Java-Class-C:java.util.BigDecimal 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 1. /* * Copyright (c) 19 ...
随机推荐
- Charles抓包 mock数据和rewrite功能
一.mock数据 mock:在后端返回异常或需要=改前端展示的数据时可以模拟返回的response 1.1 抓到接口后 右击保存response到本地,后缀改成.json打开可以看到是把json保存下 ...
- 第五周PTA笔记 后缀表达式+后缀表达式计算
后缀表达式 所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右进行(不用考虑运算符的优先级). 如:中缀表达式 3(5–2 ...
- Python基础(递归函数)
def age(n): if n == 1: return 18 else: return age(n - 1) + 2 ret=age(100) print(ret)#216 def test(nu ...
- Django笔记&教程 2-4 视图常用
Django 自学笔记兼学习教程第2章第4节--视图常用 点击查看教程总目录 1 - shortcut 视图函数需要返回一个HttpResponse对象或者其子类对象. 不过很多时候直接手写建立一个H ...
- 【故障公告】突然猛增的巨量请求冲垮一共92核CPU的k8s集群
非常抱歉,今天下午2点左右开始,博客站点突然猛增的巨量请求让k8s集群的节点服务器不堪重负,造成网站无法正常访问,由此给您带来麻烦,请您谅解. 当时k8s集群一共6台node服务器,2台32核64G, ...
- 痞子衡嵌入式:再测i.MXRT1060,1170上的普通GPIO与高速GPIO极限翻转频率
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1060/1170上的普通GPIO与高速GPIO极限翻转频率. 按照上一篇文章 <实测i.MXRT1010上的普通GP ...
- Spring Cloud Gateway自定义过滤器实战(观测断路器状态变化)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- [bzoj1432]Function
对于这n个函数,构成了$n(n-1)/2$个交点,对交点离散后,相邻两个交点间函数的编号构成了一个排列,而每一个排列第i个数所构成的段数就是第i层的段数不妨设初始在-oo处这个排列是1,2,--,n, ...
- SpringMVC注解搭配环境
1.准备文件 2.工程中的pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- KNN算法实现对iris数据集的预测
KNN算法的实现 import pandas as pd from math import dist k = int(input("请输入k值:")) dataTest = pd. ...