Java 使用 BigDecimal 的小数点后的最大位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3990701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:02:14  来源:igfitidea点击:

Maximum number of digits after the decimal point using BigDecimal

javabigdecimal

提问by Kaddy

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

BigDecimalJava中一个值的小数点后最多可以有多少位数?

采纳答案by Mark Byers

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.

它(几乎)是无限的。如果将 scale 设置为整数的最大值,您可以在小数点后存储大约 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 位整数标度组成。如果为零或正数,则刻度是小数点右侧的位数。如果为负数,则数字的未换算值乘以 10 的取反乘幂。因此,由 BigDecimal 表示的数字的值是 (unscaledValue × 10 -scale)。

回答by Luis Miguel Serrano

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

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

不可变的、任意精度的有符号十进制数。BigDecimal 由一个任意精度的整数非标度值和一个 32 位整数标度组成。如果为零或正数,则刻度是小数点右侧的位数。如果为负数,则数字的未换算值乘以 10 的取反乘幂。因此,BigDecimal 表示的数字的值是 (unscaledValue × 10^(-scale))。

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:int 数据类型是一个 32 位有符号二进制补码整数。它的最小值为 -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.

这意味着对于零或正比例数字,小数点右侧有 2,147,483,647 位数字。对于负比例数字,您将 unscaledValue 移到小数点右侧 2,147,483,648 位。