Maximum number of digits after the decimal point using BigDecimal

前端 未结 2 1630
梦毁少年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

    According to what is mentioned in the BigDecimal Java 2 Platform Standard Ed. 5.0:

    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)).

    According to Java's implementation of 32-bit integers:

    int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

    This means that for zero or positive scale numbers you have 2,147,483,647 digits to the right of the decimal point. For negative scale numbers, you have unscaledValue shifted to the right of the decimal point by 2,147,483,648 digits.

提交回复
热议问题