Rounding necessary with BigDecimal numbers

安稳与你 提交于 2019-12-03 10:23:00
brain

You have two BigDecimal numbers both of which require over a 1000 decimal places. Trying to set the scale to only have 113 decimal places means you will lose precision and therefore you need to round.

You can use the setScale methods that take a RoundingMode to prevent the exception but not the rounding.

Akostha

Try to use roudingMode of setScale method.

Some thing like:

BigDecimal aa = a.setScale(i+j, BigDecimal.ROUND_HALF_DOWN);

Rounding is necessary.

In javadoc for BigDecimal, it says BigDecimal is represented as (unscaledValue × 10-scale), where unscaledValue is an arbitatrily long integer and scale is a 32-bit integer.

2.6*10-1095 requires a scale of at least 1096 to represent accurately. It can not be represented accurately with (any integer)*10-113. Therefore, you need to providing a roundingMode.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!