A BigDecimal can represent arbitrarily large precision decimals.
It is internally represented by a pair of BigInt and UInt64: value and scale. Value contains the actual value, and scale tells the decimal point place. E.g. when value is 1234 and scale 2, the result is 12.34.
NOTE To use BigDecimal, you must explicitly import it with require "big"
The general idea and some of the arithmetic algorithms were adapted from the MIT/APACHE-licensed bigdecimal-rs.
100_u64 DEPRECATED Use DEFAULT_PRECISION instead
100_u64 Creates a new BigDecimal from BigInt value and UInt64 scale, which matches the internal representation.
Creates a new BigDecimal from Float.
Creates a new BigDecimal from BigRational.
Returns num.
Creates a new BigDecimal from Int.
Creates a new BigDecimal from a String.
Raises the decimal to the otherth power
The comparison operator.
Compares this object to other based on the receiver’s <=> method, returning true if it returns 0.
Rounds towards positive infinity.
Divides self with another BigDecimal, with an optionally configurable precision.
Divides self with another BigDecimal, with an optionally configurable precision.
DEPRECATED Use #div(other : BigDecimal, precision = DEFAULT_PRECISION) instead
Rounds towards negative infinity.
Prints this number as a String using a customizable format.
Returns true if self is an integer.
Returns the quotient as absolutely negative if self and other have different signs, otherwise returns the quotient.
Rounds this number to a given precision.
Rounds towards the nearest integer.
Rounds towards the nearest integer.
Scales a BigDecimal to another BigDecimal, so they can be computed easier.
Converts to BigFloat.
Converts to BigInt.
Converts to Float64.
Converts to Float64.
Converts to Float32.
Converts to Float32.
Converts to Float64.
Converts to Float64.
Converts to Int32.
Converts to Int32.
Converts to Int16.
Converts to Int16.
Converts to Int32.
Converts to Int32.
Converts to Int64.
Converts to Int64.
Converts to Int8.
Converts to Int8.
Prints a nicely readable and concise string representation of this object, typically intended for users, to io.
Converts to UInt32.
Converts to UInt32.
Converts to UInt16.
Converts to UInt16.
Converts to UInt32.
Converts to UInt32.
Converts to UInt64.
Converts to UInt64.
Converts to UInt8.
Converts to UInt8.
Rounds towards zero.
Returns true if self is equal to zero.
Comparable(BigDecimal)
Comparable(BigRational)
Comparable(Float)
Comparable(Int)
Number
Number
Number
Number
Comparable(BigFloat)
Steppable
Comparable(Number)
Value
Object
Object
Object
Creates a new BigDecimal from BigInt value and UInt64 scale, which matches the internal representation.
Creates a new BigDecimal from Float.
NOTE Floats are fundamentally less precise than BigDecimals, which makes initialization from them risky.
Creates a new BigDecimal from BigRational.
NOTE BigRational are fundamentally more precise than BigDecimals, which makes initialization from them risky.
Returns num. Useful for generic code that does T.new(...) with T being a Number.
Creates a new BigDecimal from Int.
Creates a new BigDecimal from a String.
Allows only valid number strings with an optional negative sign.
Raises the decimal to the otherth power
require "big" BigDecimal.new(1234, 2) ** 2 # => 152.2756
The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.
Subclasses define this method to provide class-specific ordering.
The comparison operator is usually used to sort values:
# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]
# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3] Compares this object to other based on the receiver’s <=> method, returning true if it returns 0.
Also returns true if this and other are the same object.
Rounds towards positive infinity.
Divides self with another BigDecimal, with an optionally configurable precision.
When the division is inexact, the returned value rounds towards negative infinity, and its scale is never greater than scale - other.scale + precision.
BigDecimal.new(1).div(BigDecimal.new(2)) # => BigDecimal(@value=5, @scale=2) BigDecimal.new(1).div(BigDecimal.new(3), 5) # => BigDecimal(@value=33333, @scale=5)
Divides self with another BigDecimal, with an optionally configurable precision.
When the division is inexact, the returned value rounds towards negative infinity, and its scale is never greater than scale - other.scale + precision.
BigDecimal.new(1).div(BigDecimal.new(2)) # => BigDecimal(@value=5, @scale=2) BigDecimal.new(1).div(BigDecimal.new(3), 5) # => BigDecimal(@value=33333, @scale=5)
DEPRECATED Use #div(other : BigDecimal, precision = DEFAULT_PRECISION) instead
Rounds towards negative infinity.
Prints this number as a String using a customizable format.
separator is used as decimal separator, delimiter as thousands delimiter between batches of group digits.
If decimal_places is nil, all significant decimal places are printed (similar to #to_s). If the argument has a numeric value, the number of visible decimal places will be fixed to that amount.
Trailing zeros are omitted if only_significant is true.
123_456.789.format # => "123,456.789"
123_456.789.format(',', '.') # => "123.456,789"
123_456.789.format(decimal_places: 2) # => "123,456.79"
123_456.789.format(decimal_places: 6) # => "123,456.789000"
123_456.789.format(decimal_places: 6, only_significant: true) # => "123,456.789" Returns true if self is an integer.
Non-integer types may return true as long as self denotes a finite value without any fractional parts.
1.integer? # => true 1.0.integer? # => true 1.2.integer? # => false (1 / 0).integer? # => false (0 / 0).integer? # => false
Returns the quotient as absolutely negative if self and other have different signs, otherwise returns the quotient.
Rounds this number to a given precision.
Rounds to the specified number of digits after the decimal place, (or before if negative), in base base.
The rounding mode controls the direction of the rounding. The default is RoundingMode::TIES_EVEN which rounds to the nearest integer, with ties (fractional value of 0.5) being rounded to the even neighbor (Banker's rounding).
-1763.116.round(2) # => -1763.12
Rounds towards the nearest integer. If both neighboring integers are equidistant, rounds away from zero.
Rounds towards the nearest integer. If both neighboring integers are equidistant, rounds towards the even neighbor (Banker's rounding).
Scales a BigDecimal to another BigDecimal, so they can be computed easier.
Converts to BigInt. Truncates anything on the right side of the decimal point.
Converts to Float64. Raises OverflowError in case of overflow.
Converts to Float32. Raises OverflowError in case of overflow.
Converts to Float64. Raises OverflowError in case of overflow.
Converts to Int32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to Int32. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.
Converts to Int16. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to Int16. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.
Converts to Int32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to Int32. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.
Converts to Int64. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to Int64. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.
Converts to Int8. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to Int8. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.
Prints a nicely readable and concise string representation of this object, typically intended for users, to io.
This method is called when an object is interpolated in a string literal:
"foo #{bar} baz" # calls bar.to_io with the builder for this string IO#<< calls this method to append an object to itself:
io << bar # calls bar.to_s(io)
Thus implementations must not interpolate self in a string literal or call io << self which both would lead to an endless loop.
Also see #inspect(IO).
Converts to UInt32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to UInt32. Truncates anything on the right side of the decimal point, converting negative to positive. In case of overflow a wrapping is performed.
Converts to UInt16. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to UInt16. Truncates anything on the right side of the decimal point, converting negative to positive. In case of overflow a wrapping is performed.
Converts to UInt32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to UInt32. Truncates anything on the right side of the decimal point, converting negative to positive. In case of overflow a wrapping is performed.
Converts to UInt64. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to UInt64. Truncates anything on the right side of the decimal point, converting negative to positive. In case of overflow a wrapping is performed.
Converts to UInt8. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.
Converts to UInt8. Truncates anything on the right side of the decimal point, converting negative to positive. In case of overflow a wrapping is performed.
Rounds towards zero.
Returns true if self is equal to zero.
0.zero? # => true 5.zero? # => false
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/BigDecimal.html