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
Constructors
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 a String.
Allows only valid number strings with an optional negative sign.
Class methods
Instance methods
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.
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)
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 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.