BigDecimalAn exact base-10 decimal number with up to 28 significant digits -- for money and other quantities where binary floating point drifts. Construct with BigDecimal.of: (a String or an Integer; deliberately not a Double, which would already carry rounding error). Arithmetic never mixes silently: a non-BigDecimal operand is 'message not understood'.
0.1 + 0.2 "* -> 0.30000000000000004 (BigDecimal.of:'0.1') + (BigDecimal.of:'0.2') "* -> 0.3
A BigDecimal parsed exactly from a decimal String (trailing zeros keep their scale: '1.50' has scale 2) or converted exactly from an Integer. A Double is intentionally not accepted -- go through a String so the value is not already corrupted by binary rounding. A non-numeric String raises a ValueError.
BigDecimal.of:'1.50' "* -> 1.50
A BigDecimal from an integer mantissa and a scale -- the number of fractional digits, 0 through 28.
BigDecimal.of:150 scale:2 "* -> 1.50
The exact product of two BigDecimals (overflow-checked like +).
The exact sum of two BigDecimals; raises an ArithmeticError if the result overflows the 28-digit capacity.
(BigDecimal.of:'0.1') + (BigDecimal.of:'0.2') "* -> 0.3
The exact difference of two BigDecimals (overflow-checked like +).
The quotient of two BigDecimals; a non-terminating quotient is rounded at the 28-digit precision limit. Raises an ArithmeticError for a zero divisor or on overflow.
Whether the receiver is less than the BigDecimal argument. The one native comparison -- >, <= and >= derive from it.
Whether the argument is a numerically equal BigDecimal -- scale is ignored, so 1.50 equals 1.5. Any other type is simply unequal, never an error.
(BigDecimal.of:'1.50') == (BigDecimal.of:'1.5') "* -> true
The absolute value.
(BigDecimal.of:'-1.5').abs "* -> 1.5
Convert to a Double, accepting binary rounding error.
(BigDecimal.of:'1.5').asDouble "* -> 1.5
Truncate toward zero to a 64-bit Integer; raises an ArithmeticError if the result is out of range.
(BigDecimal.of:'3.99').asInteger "* -> 3
Round to the given number of fractional digits (0 through 28), halves away from zero -- the same rule as Double.round, not banker's rounding.
(BigDecimal.of:'2.345').round:2 "* -> 2.35 (BigDecimal.of:'-2.5').round:0 "* -> -3
The exact decimal digits, preserving scale (a value of scale 2 prints as e.g. '1.50').
The number of fractional digits.
(BigDecimal.of:'1.50').scale "* -> 2