A BigFloat can represent arbitrarily large floats.
It is implemented under the hood with GMP.
NOTE To use BigFloat, you must explicitly import it with require "big"
Negates this value's sign.
The comparison operator.
The comparison operator.
Returns the absolute value of this number.
Rounds towards positive infinity.
Rounds towards negative infinity.
Prints this number as a String using a customizable format.
Checks whether this value is infinite.
Returns true if self is an integer.
Returns whether this value is a not-a-number.
Rounds towards the nearest integer.
Rounds towards the nearest integer.
Prints a nicely readable and concise string representation of this object, typically intended for users, to io.
Rounds towards zero.
Comparable(Float)
Comparable(BigFloat)
Comparable(Int)
Float
Float
Comparable(BigDecimal)
Comparable(BigRational)
Comparable(BigInt)
Number
Number
Number
Number
Comparable(BigFloat)
Steppable
Comparable(Number)
Value
Object
Object
Object
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] 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] Returns the absolute value of this number.
123.abs # => 123 -123.abs # => 123
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" Checks whether this value is infinite. Returns 1 if this value is positive infinity, -1 if this value is negative infinity, or nil otherwise.
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 whether this value is a not-a-number.
This includes both quiet and signalling NaNs from IEEE 754.
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).
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).
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/BigFloat.html