Maximum number of digits after the decimal point using BigDecimal

前端 未结 2 1617
梦毁少年i
梦毁少年i 2021-02-19 13:32

What is the maximum number of digits we can have after the decimal point of a BigDecimal value in Java?

2条回答
  •  攒了一身酷
    2021-02-19 13:50

    It's (almost) unlimited. You can store roughly 2 billion digits after the decimal point if scale is set to the maximum value of an integer, although you may run out of memory if you try to do this. If you need to store so many digits that the limit is a problem then you probably need to rethink the design of your program.

    See the BigDecimal documentation:

    Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).

提交回复
热议问题