Rounding necessary with BigDecimal numbers

前端 未结 3 1767
悲&欢浪女
悲&欢浪女 2021-02-06 21:00

I want to set scale of two BigDecimal numbers a and b . as in this example :

BigDecimal a = new BigDecimal(\"2.6E-1095\");
        BigD         


        
3条回答
  •  春和景丽
    2021-02-06 21:57

    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.

提交回复
热议问题