struct

BigDecimal

Inherits Comparable / Comparable / Comparable / Comparable / Number / Comparable / Steppable / Comparable / Value / Object

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.

Constants

DEFAULT_MAX_DIV_ITERATIONS = 100_u64
DEFAULT_PRECISION = 100_u64

Constructors

new(value : BigInt, scale : UInt64)

Creates a new BigDecimal from BigInt value and UInt64 scale, which matches the internal representation.

Source
new(num : Float) : self

Creates a new BigDecimal from Float.

NOTE: Floats are fundamentally less precise than BigDecimals, which makes initialization from them risky.

Source
new(num : BigRational) : self

Creates a new BigDecimal from BigRational.

NOTE: BigRational are fundamentally more precise than BigDecimals, which makes initialization from them risky.

Source
new(num : BigDecimal) : self

Returns num. Useful for generic code that does T.new(...) with T being a Number.

Source
new(num : Int = 0, scale : Int = 0)

Creates a new BigDecimal from Int.

Source
new(str : String)

Creates a new BigDecimal from a String.

Allows only valid number strings with an optional negative sign.

Source
new(pull : JSON::PullParser) : self
Source

Class methods

from_json_object_key?(key : String) : BigDecimal | Nil
Source

Instance methods

%(other : Int)
Source
**(other : Int) : BigDecimal

Raises the decimal to the otherth power

require "big"

BigDecimal.new(1234, 2) ** 2 # => 152.2756
Source
/(other : Int8) : BigDecimal
Source
/(other : UInt8) : BigDecimal
Source
/(other : Int16) : BigDecimal
Source
/(other : Int32) : BigDecimal
Source
/(other : Int64) : BigDecimal
Source
<=>(other : BigDecimal) : Int32

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]
Source
<=>(other : BigRational) : Int32
Source
<=>(other : Float::Primitive) : Int32 | Nil
Source
<=>(other : BigFloat) : Int32
Source
<=>(other : Int) : Int32
Source
==(other : BigDecimal) : Bool

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.

Source
ceil

Rounds towards positive infinity.

Source
clone
Source
div(other : BigDecimal, precision : Int = DEFAULT_PRECISION) : BigDecimal

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)
Source
div(other : BigDecimal, *, max_div_iterations = DEFAULT_MAX_DIV_ITERATIONS) : BigDecimal

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)
Source
floor

Rounds towards negative infinity.

Source
format(io : IO, separator = '.', delimiter = ',', decimal_places : Int | Nil = nil, *, group : Int = 3, only_significant : Bool = false) : Nil

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"
Source
integer?

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
Source
normalize_quotient(other : BigDecimal, quotient : BigInt) : BigInt

Returns the quotient as absolutely negative if self and other have different signs, otherwise returns the quotient.

Source
round(digits : Number, base = 10, *, mode : RoundingMode = :ties_even) : BigDecimal

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
Source
round_away

Rounds towards the nearest integer. If both neighboring integers are equidistant, rounds away from zero.

Source
round_even

Rounds towards the nearest integer. If both neighboring integers are equidistant, rounds towards the even neighbor (Banker's rounding).

Source
scale
Source
scale_to(new_scale : BigDecimal) : BigDecimal

Scales a BigDecimal to another BigDecimal, so they can be computed easier.

Source
to_big_d
Source
to_big_f

Converts to BigFloat.

Source
to_big_i

Converts to BigInt. Truncates anything on the right side of the decimal point.

Source
to_big_r
Source
to_f

Converts to Float64. Raises OverflowError in case of overflow.

Source
to_f!

Converts to Float64. In case of overflow a wrapping is performed.

Source
to_f32

Converts to Float32. Raises OverflowError in case of overflow.

Source
to_f32!

Converts to Float32. In case of overflow a wrapping is performed.

Source
to_f64

Converts to Float64. Raises OverflowError in case of overflow.

Source
to_f64!

Converts to Float64. In case of overflow a wrapping is performed.

Source
to_i

Converts to Int32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_i!

Converts to Int32. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.

Source
to_i16

Converts to Int16. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_i16!

Converts to Int16. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.

Source
to_i32

Converts to Int32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_i32!

Converts to Int32. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.

Source
to_i64

Converts to Int64. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_i64!

Converts to Int64. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.

Source
to_i8

Converts to Int8. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_i8!

Converts to Int8. Truncates anything on the right side of the decimal point. In case of overflow a wrapping is performed.

Source
to_json(json : JSON::Builder) : Nil
Source
to_json_object_key
Source
to_s(io : IO) : Nil

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).

Source
to_u

Converts to UInt32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_u!

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.

Source
to_u16

Converts to UInt16. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_u16!

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.

Source
to_u32

Converts to UInt32. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_u32!

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.

Source
to_u64

Converts to UInt64. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_u64!

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.

Source
to_u8

Converts to UInt8. Truncates anything on the right side of the decimal point. Raises OverflowError in case of overflow.

Source
to_u8!

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.

Source
trunc

Rounds towards zero.

Source
value
Source
zero?

Returns true if self is equal to zero.

0.zero? # => true
5.zero? # => false
Source