ArithmeticException:“非终止十进制扩展;没有精确可表示的十进制结果”
为什么以下代码会引发如下所示的异常?
BigDecimal a = new BigDecimal("1.6");
BigDecimal b = new BigDecimal("9.2");
a.divide(b) // results in the following exception.
例外:
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
Why does the following code raise the exception shown below?
BigDecimal a = new BigDecimal("1.6");
BigDecimal b = new BigDecimal("9.2");
a.divide(b) // results in the following exception.
Exception:
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
来自 Java 11 <代码>BigDecimal文档:
要解决此问题,您需要执行以下操作:
其中 2 是比例,RoundingMode.HALF_UP 是舍入模式
有关更多详细信息,请参阅 此博文。
From the Java 11
BigDecimal
docs:To fix, you need to do something like this:
where 2 is the scale and RoundingMode.HALF_UP is rounding mode
For more details see this blog post.
因为您没有指定精度和舍入模式。 BigDecimal 抱怨它可以使用 10、20、5000 或无限小数位,但它仍然无法为您提供数字的精确表示。因此,它不会给你一个不正确的 BigDecimal,而是会抱怨你。
但是,如果您提供 RoundingMode 和精度,那么它将能够转换(例如 1.333333333-to-infinity 到 1.3333 之类的东西......但是您作为程序员需要告诉它您对什么精度感到满意'。
Because you're not specifying a precision and a rounding-mode. BigDecimal is complaining that it could use 10, 20, 5000, or infinity decimal places, and it still wouldn't be able to give you an exact representation of the number. So instead of giving you an incorrect BigDecimal, it just whinges at you.
However, if you supply a RoundingMode and a precision, then it will be able to convert (eg. 1.333333333-to-infinity to something like 1.3333 ... but you as the programmer need to tell it what precision you're 'happy with'.
您可以
选择您想要的位数:32、64 或 128。
查看此链接:
http://edelstein.pebbles.cs.cmu.edu/jadeite/main.php?api=java6&state=类&package=java.math&class=MathContext
You can do
You can choose the number of bits you want: either 32, 64 or 128.
Check out this link :
http://edelstein.pebbles.cs.cmu.edu/jadeite/main.php?api=java6&state=class&package=java.math&class=MathContext
为了解决这个问题,我使用了下面的代码,
其中 2 是比例。现在问题应该解决了。
To fix such an issue I have used the below code
Where 2 is scale. Now the problem should be resolved.
这是一个四舍五入结果的问题,我的解决方案如下。
It´s a issue of rounding the result, the solution for me is the following.
我遇到了同样的问题,因为我的代码行是:
我更改为这个,阅读之前的答案,因为我没有写十进制精度:
4是十进制精度
和RoundingMode是枚举常量,您可以选择其中任何一个
UP、DOWN、CEILING、FLOOR、HALF_DOWN、HALF_EVEN、HALF_UP
在这种情况下 HALF_UP,将得到以下结果:
您可以在此处检查
RoundingMode
信息:http://www.javabeat.net/precise-rounding-of-小数使用舍入模式枚举/I had this same problem, because my line of code was:
I change to this, reading previous Answer, because I was not writing decimal precision:
4 is Decimal Precison
AND RoundingMode are Enum constants, you could choose any of this
UP, DOWN, CEILING, FLOOR, HALF_DOWN, HALF_EVEN, HALF_UP
In this Case HALF_UP, will have this result:
You can check the
RoundingMode
information here: http://www.javabeat.net/precise-rounding-of-decimals-using-rounding-mode-enumeration/回答 BigDecimal throws ArithmeticException
在您的 除法调用中添加 MathContext 对象并调整精度和舍入模式。这应该可以解决你的问题
Answer for BigDecimal throws ArithmeticException
Add MathContext object in your divide method call and adjust precision and rounding mode. This should fix your problem
您的程序不知道要使用十进制数的精度,因此会抛出:
绕过异常的解决方案:
Your program does not know what precision for decimal numbers to use so it throws:
Solution to bypass exception:
对我来说,它正在与此一起工作:
For me, it's working with this: