Reputation: 1946
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.
Can anyone explain me the meaning of 'precision' and 'scale' for a BigDecimal value?
@Column(precision = 11, scale = 2)
Thanks!
Upvotes: 122
Views: 174878
Reputation: 123
The JPA annotation @Column(precision = 11, scale = 2)
for BigDecimal
variable translates to DECIMAL(11,2)
in the MYSQL database.
This means, the column can store a value having 11 digits in total, with 2 digits allowed after the decimal. ie. the Range here would be [-999999999.99,999999999.99]
Upvotes: 0
Reputation: 8585
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 .
Precision:
The precision is the number of digits in the unscaled value. For instance, for the number 123.45, the precision returned is 5.
So, precision indicates the length of the arbitrary precision integer. Here are a few examples of numbers with the same scale, but different precision:
In the special case that the number is equal to zero (i.e. 0.000), the precision is always 1.
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. For example, a scale of -3 means the unscaled value is multiplied by 1000.
This means that the integer value of the ‘BigDecimal’ is multiplied by .
Here are a few examples of the same precision, with different scales:
BigDecimal.toString:
The toString
method for a BigDecimal
behaves differently based on the scale and precision
. (Thanks to @RudyVelthuis for pointing this out.)
scale == 0
, the integer is just printed out, as-is.scale < 0
, E-Notation is always used (e.g. 5 scale -1 produces "5E+1")scale >= 0
and precision - scale -1 >= -6
a plain decimal number is produced (e.g. 10000000 scale 1 produces "1000000.0")precision - scale -1
equals More examples:
Upvotes: 178
Reputation: 845
Precision is the total number of significant digits in a number. Scale is the number of digits to the right of the decimal point.
Examples:
123.456 Precision=6 Scale=3
10 Precision=2 Scale=0
-96.9923 Precision=6 Scale=4
0.0 Precision=1 Scale=1
Negative Scale
For a negative scale value, we apply the following formula: result = (given number) * 10 ^ (-(scale value)) Example
Given number = 1234.56
scale = -5
-> (1234.56) * 10^(-(-5))
-> (1234.56) * 10^(+5)
-> 123456000
Reference: https://www.logicbig.com/quick-info/programming/precision-and-scale.html
Upvotes: 7
Reputation: 3109
Precision: Total number of significant digits
Scale: Number of digits to the right of the decimal point
See BigDecimal
class documentation for details.
Upvotes: 95
Reputation: 2874
From your example annotation the maximum digits is 2 after the decimal point and 9 before (totally 11):
123456789,01
Upvotes: 3
Reputation: 140554
Quoting 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.
Upvotes: 4