Java BigDecimal、精度和小数位数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35435691/
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
BigDecimal, precision and scale
提问by jpadilladev
I'm using BigDecimal for my numbers in my application, for example, with JPA. I did a bit of researching about the terms 'precision' and 'scale' but I don't understand what are they exactly.
我在我的应用程序中为我的数字使用 BigDecimal,例如,使用 JPA。我对“精度”和“规模”这两个术语进行了一些研究,但我不明白它们到底是什么。
Can anyone explain me the meaning of 'precision' and 'scale' for a BigDecimal value?
谁能向我解释 BigDecimal 值的“精度”和“比例”的含义?
@Column(precision = 11, scale = 2)
Thanks!
谢谢!
采纳答案by Austin D
A BigDecimal
is defined by two values: an arbitrary precision integer and a 32-bit integer scale. The value of the BigDecimal
is defined to be .
ABigDecimal
由两个值定义:任意精度整数和 32 位整数scale。的值BigDecimal
定义为。
Precision:
精确:
The precisionis the number of digits in the unscaled value. For instance, for the number 123.45, the precision returned is 5.
的精度是在标度值的位数。例如,对于数字 123.45,返回的精度为 5。
So, precisionindicates the length of the arbitrary precision integer. Here are a few examples of numbers with the same scale, but different precision:
因此,precision表示任意精度整数的长度。以下是几个具有相同比例但精度不同的数字示例:
- 12345 / 100000 = 0.12345 // scale = 5, precision = 5
- 12340 / 100000 = 0.1234 // scale = 5, precision = 4
- 1 / 100000 = 0.00001 // scale = 5, precision = 1
- 12345 / 100000 = 0.12345 // 比例 = 5,精度 = 5
- 12340 / 100000 = 0.1234 // 比例 = 5,精度 = 4
- 1 / 100000 = 0.00001 // 比例 = 5,精度 = 1
In the special case that the number is equal to zero (i.e. 0.000), the precision is always 1.
在数字等于 0(即 0.000)的特殊情况下,精度始终为 1。
Scale:
规模:
If zero or positive, the scaleis 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. For example, a scale of -3 means the unscaled value is multiplied by 1000.
如果为零或正数,则刻度是小数点右侧的位数。如果为负数,则数字的未换算值乘以 10 的取反乘幂。例如,比例为 -3 表示未缩放的值乘以 1000。
This means that the integer value of the ‘BigDecimal' is multiplied by .
这意味着“BigDecimal”的整数值乘以。
Here are a few examples of the same precision, with different scales:
以下是一些具有相同精度但具有不同比例的示例:
- 12345 with scale 5 = 0.12345
- 12345 with scale 4 = 1.2345
- …
- 12345 with scale 0 = 12345
- 12345 with scale -1 = 123450 ?
- 12345 比例 5 = 0.12345
- 12345 比例 4 = 1.2345
- …
- 12345 比例为 0 = 12345
- 12345 与规模 -1 = 123450 ?
BigDecimal.toString:
BigDecimal.toString:
The toString
method for a BigDecimal
behaves differently based on the scale and precision
. (Thanks to @RudyVelthuis for pointing this out.)
a 的toString
方法BigDecimal
根据比例和 表现不同precision
。(感谢@RudyVelthuis 指出这一点。)
- If
scale == 0
, the integer is just printed out, as-is. - If
scale < 0
, E-Notation is always used (e.g. 5 scale -1 produces "5E+1") - If
scale >= 0
andprecision - scale -1 >= -6
a plain decimal number is produced (e.g. 10000000 scale 1 produces "1000000.0") - Otherwise, E-notation is used, e.g. 10 scale 8 produces "1.0E-7" since
precision - scale -1
equalsis less than -6.
- 如果
scale == 0
,则按原样打印出整数。 - 如果
scale < 0
,则始终使用 E-Notation(例如 5 scale -1 产生“5E+1”) - 如果
scale >= 0
和precision - scale -1 >= -6
产生一个普通的十进制数(例如千万刻度1产生“1000000.0”) - 否则,使用 E-notation,例如 10 scale 8 产生“1.0E-7”,因为
precision - scale -1
equals小于 -6。
More examples:
更多例子:
- 19/100 = 0.19 // integer=19, scale=2, precision=2
- 1/1000 = 0.0001 // integer=1, scale = 4, precision = 1
- 19/100 = 0.19 // 整数=19,小数位数=2,精度=2
- 1/1000 = 0.0001 // 整数 = 1,小数位数 = 4,精度 = 1
回答by Andy Turner
Quoting Javadoc:
引用Javadoc:
The precision is the number of digits in the unscaled value.
精度是未换算值的位数。
and
和
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. For example, a scale of -3 means the unscaled value is multiplied by 1000.
如果为零或正数,则刻度是小数点右侧的位数。如果为负数,则数字的未换算值乘以 10 的取反乘幂。例如,比例为 -3 表示未缩放的值乘以 1000。
回答by hamena314
Precision:Total number of significant digits
Scale:Number of digits to the right of the decimal point
精度:有效数字的总数
Scale:小数点右边的位数
See BigDecimal
class documentation for details.
有关BigDecimal
详细信息,请参阅类文档。
回答by adranale
From your example annotation the maximum digits is 2 after the decimal point and 9 before (totally 11):
123456789,01
从您的示例注释中,最大位数是小数点后 2 位,小数点前 9 位(总共 11 位):
123456789,01