使用BigDecimal后的最大位数

时间:2021-10-21 17:08:40

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

在Java中,在BigDecimal值之后,我们可以拥有的最大位数是多少?

2 个解决方案

#1


13  

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.

(几乎)无限。如果将比例设置为一个整数的最大值,那么可以在小数点后存储大约20亿位数字,尽管如果尝试这样做,可能会耗尽内存。如果您需要存储如此多的数字,以至于限制是一个问题,那么您可能需要重新考虑程序的设计。

See the BigDecimal documentation:

看到BigDecimal文档:

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

不可变的,任意精度的有符号小数。BigDecimal是由任意精度的无标度值和32位整数标度组成的。如果是0或正值,标度就是小数点右边的位数。如果是负数,则该数字的未缩放值乘以10的比例负数的乘幂。的价值所代表的数量因此BigDecimal(unscaledValue×10-scale)。

#2


2  

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

根据BigDecimal Java 2平台标准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)).

不可变的,任意精度的有符号小数。BigDecimal是由任意精度的无标度值和32位整数标度组成的。如果是0或正值,标度就是小数点右边的位数。如果是负数,则该数字的未缩放值乘以10的比例负数的乘幂。的价值所代表的数量因此BigDecimal(unscaledValue×10 ^(剂量))。

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

根据Java实现的32位整数:

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.

int型数据类型是一个32位的符号2的补体整数。它的最小值为-2,147,483,648,最大值为2,147,483,647(包括在内)。对于整数值,这种数据类型通常是默认的选择,除非有理由(如上面所示)选择其他数据类型。这种数据类型很可能足够大,可以满足程序使用的数字,但如果需要更大的值范围,则使用long。

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.

这意味着,对于0或正刻度的数字,小数点右边有2,147,483,647位。对于负刻度数,您有未缩放值的小数点右移2147,483,648位。

#1


13  

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.

(几乎)无限。如果将比例设置为一个整数的最大值,那么可以在小数点后存储大约20亿位数字,尽管如果尝试这样做,可能会耗尽内存。如果您需要存储如此多的数字,以至于限制是一个问题,那么您可能需要重新考虑程序的设计。

See the BigDecimal documentation:

看到BigDecimal文档:

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

不可变的,任意精度的有符号小数。BigDecimal是由任意精度的无标度值和32位整数标度组成的。如果是0或正值,标度就是小数点右边的位数。如果是负数,则该数字的未缩放值乘以10的比例负数的乘幂。的价值所代表的数量因此BigDecimal(unscaledValue×10-scale)。

#2


2  

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

根据BigDecimal Java 2平台标准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)).

不可变的,任意精度的有符号小数。BigDecimal是由任意精度的无标度值和32位整数标度组成的。如果是0或正值,标度就是小数点右边的位数。如果是负数,则该数字的未缩放值乘以10的比例负数的乘幂。的价值所代表的数量因此BigDecimal(unscaledValue×10 ^(剂量))。

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

根据Java实现的32位整数:

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.

int型数据类型是一个32位的符号2的补体整数。它的最小值为-2,147,483,648,最大值为2,147,483,647(包括在内)。对于整数值,这种数据类型通常是默认的选择,除非有理由(如上面所示)选择其他数据类型。这种数据类型很可能足够大,可以满足程序使用的数字,但如果需要更大的值范围,则使用long。

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.

这意味着,对于0或正刻度的数字,小数点右边有2,147,483,647位。对于负刻度数,您有未缩放值的小数点右移2147,483,648位。