1
votes

The first paragraph of the javadoc for BigDecimal says:

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)

Can someone explain what this means, especially the bit starting 'If negative', but preferably all of it? I think that 'scale' means the numbers after the decimal point, but does that mean the 'unscaled' is the rest of it?

1
Do you know what float and/or double are? I mean in a way they are stored.PM 77-1
No. I know it's not good to use them for calculations where precision is important, but I don't know how they are stored internally.user384842

1 Answers

4
votes

Scale means the power of ten by which the magnitude of the number is multiplied. The power could be positive or negative. When the power is negative k, the corresponding negative power of ten is a fraction with 1 in the numerator and the denominator is 10k. To put this simply, positive scale k means multiplying by 10 k times; negative scale of -k means dividing by 10 k times.

Here are some examples:

magnitude scale    value
--------- ----- -------------
1.2345678   -3   0.0012345678
1.2345678   -2   0.012345678
1.2345678   -1   0.12345678
1.2345678    0   1.2345678
1.2345678    1   12.345678
1.2345678    2   123.45678
1.2345678    3   1234.5678

Note : The dot in the magnitude above is implied. BigDecimal stores the magnitude of the number as an arbitrarily large integer.