Number
Inherits Comparable / Steppable / Comparable / Value / Object
The top-level number type.
Constants
Default SI prefixes ordered by magnitude.
Constructors
Returns the additive identity of this type.
For numerical types, it is the value 0 expressed in the respective type.
Int32.additive_identity # => 0
Float64.additive_identity # => 0.0
Returns the multiplicative identity of this type.
For numerical types, it is the value 1 expressed in the respective type.
Int32.multiplicative_identity # => 1
Float64.multiplicative_identity # => 1.0
Class methods
Instance methods
Divides self by other using floored division.
The result will be of the same type as self.
The comparison operator.
Returns:
-1ifselfis less than other0ifselfis equal to other1ifselfis greater than othernilifselfisNaNor other isNaN, becauseNaNvalues are not comparable
Cis is a mathematical notation representing cos x + i sin x.
Returns a Complex object with real part Math.cos(self) and imaginary part Math.sin(self), where self represents the angle in radians.
Returns a Tuple of two elements containing the quotient
and modulus obtained by dividing self by number.
11.divmod(3) # => {3, 2}
11.divmod(-3) # => {-4, -1}
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"
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"
Pretty prints this number as a String in a human-readable format.
This is particularly useful if a number can have a wide value range and the exact value is less relevant.
It rounds the number to the nearest thousands magnitude with precision
number of significant digits. The order of magnitude is expressed with an
appended quantifier.
By default, SI prefixes are used (see SI_PREFIXES).
1_200_000_000.humanize # => "1.2G"
0.000_000_012.humanize # => "12.0n"
If significant is false, the number of precision digits is preserved
after the decimal separator.
1_234.567_890.humanize(precision: 2) # => "1.2k"
1_234.567_890.humanize(precision: 2, significant: false) # => "1.23k"
separator describes the decimal separator, delimiter the thousands
delimiter (see #format).
unit_separator is inserted between the value and the unit. Users are encouraged to use a non-breaking space ('\u00A0') to prevent output being split across lines.
See Int#humanize_bytes to format a file size.
Pretty prints this number as a String in a human-readable format.
This is particularly useful if a number can have a wide value range and the exact value is less relevant.
It rounds the number to the nearest thousands magnitude with precision
number of significant digits. The order of magnitude is expressed with an
appended quantifier.
By default, SI prefixes are used (see SI_PREFIXES).
1_200_000_000.humanize # => "1.2G"
0.000_000_012.humanize # => "12.0n"
If significant is false, the number of precision digits is preserved
after the decimal separator.
1_234.567_890.humanize(precision: 2) # => "1.2k"
1_234.567_890.humanize(precision: 2, significant: false) # => "1.23k"
separator describes the decimal separator, delimiter the thousands
delimiter (see #format).
This methods yields the order of magnitude and self and expects the block
to return a Tuple(Int32, _) containing the (adjusted) magnitude and unit.
The magnitude is typically adjusted to a multiple of 3.
def humanize_length(number)
number.humanize do |magnitude, number|
case magnitude
when -2, -1 then {-2, " cm"}
when .>=(4)
{3, " km"}
else
magnitude = Number.prefix_index(magnitude)
{magnitude, " #{Number.si_prefix(magnitude)}m"}
end
end
end
humanize_length(1_420) # => "1.42 km"
humanize_length(0.23) # => "23.0 cm"
See Int#humanize_bytes to format a file size.
Pretty prints this number as a String in a human-readable format.
This is particularly useful if a number can have a wide value range and the exact value is less relevant.
It rounds the number to the nearest thousands magnitude with precision
number of significant digits. The order of magnitude is expressed with an
appended quantifier.
By default, SI prefixes are used (see SI_PREFIXES).
1_200_000_000.humanize # => "1.2G"
0.000_000_012.humanize # => "12.0n"
If significant is false, the number of precision digits is preserved
after the decimal separator.
1_234.567_890.humanize(precision: 2) # => "1.2k"
1_234.567_890.humanize(precision: 2, significant: false) # => "1.23k"
separator describes the decimal separator, delimiter the thousands
delimiter (see #format).
unit_separator is inserted between the value and the unit. Users are encouraged to use a non-breaking space ('\u00A0') to prevent output being split across lines.
See Int#humanize_bytes to format a file size.
Pretty prints this number as a String in a human-readable format.
This is particularly useful if a number can have a wide value range and the exact value is less relevant.
It rounds the number to the nearest thousands magnitude with precision
number of significant digits. The order of magnitude is expressed with an
appended quantifier.
By default, SI prefixes are used (see SI_PREFIXES).
1_200_000_000.humanize # => "1.2G"
0.000_000_012.humanize # => "12.0n"
If significant is false, the number of precision digits is preserved
after the decimal separator.
1_234.567_890.humanize(precision: 2) # => "1.2k"
1_234.567_890.humanize(precision: 2, significant: false) # => "1.23k"
separator describes the decimal separator, delimiter the thousands
delimiter (see #format).
This methods yields the order of magnitude and self and expects the block
to return a Tuple(Int32, _) containing the (adjusted) magnitude and unit.
The magnitude is typically adjusted to a multiple of 3.
def humanize_length(number)
number.humanize do |magnitude, number|
case magnitude
when -2, -1 then {-2, " cm"}
when .>=(4)
{3, " km"}
else
magnitude = Number.prefix_index(magnitude)
{magnitude, " #{Number.si_prefix(magnitude)}m"}
end
end
end
humanize_length(1_420) # => "1.42 km"
humanize_length(0.23) # => "23.0 cm"
See Int#humanize_bytes to format a file size.
Pretty prints this number as a String in a human-readable format.
This is particularly useful if a number can have a wide value range and the exact value is less relevant.
It rounds the number to the nearest thousands magnitude with precision
number of significant digits. The order of magnitude is expressed with an
appended quantifier.
By default, SI prefixes are used (see SI_PREFIXES).
1_200_000_000.humanize # => "1.2G"
0.000_000_012.humanize # => "12.0n"
If significant is false, the number of precision digits is preserved
after the decimal separator.
1_234.567_890.humanize(precision: 2) # => "1.2k"
1_234.567_890.humanize(precision: 2, significant: false) # => "1.23k"
separator describes the decimal separator, delimiter the thousands
delimiter (see #format).
This methods yields the order of magnitude and self and expects the block
to return a Tuple(Int32, _) containing the (adjusted) magnitude and unit.
The magnitude is typically adjusted to a multiple of 3.
def humanize_length(number)
number.humanize do |magnitude, number|
case magnitude
when -2, -1 then {-2, " cm"}
when .>=(4)
{3, " km"}
else
magnitude = Number.prefix_index(magnitude)
{magnitude, " #{Number.si_prefix(magnitude)}m"}
end
end
end
humanize_length(1_420) # => "1.42 km"
humanize_length(0.23) # => "23.0 cm"
See Int#humanize_bytes to format a file size.
Pretty prints this number as a String in a human-readable format.
This is particularly useful if a number can have a wide value range and the exact value is less relevant.
It rounds the number to the nearest thousands magnitude with precision
number of significant digits. The order of magnitude is expressed with an
appended quantifier.
By default, SI prefixes are used (see SI_PREFIXES).
1_200_000_000.humanize # => "1.2G"
0.000_000_012.humanize # => "12.0n"
If significant is false, the number of precision digits is preserved
after the decimal separator.
1_234.567_890.humanize(precision: 2) # => "1.2k"
1_234.567_890.humanize(precision: 2, significant: false) # => "1.23k"
separator describes the decimal separator, delimiter the thousands
delimiter (see #format).
This methods yields the order of magnitude and self and expects the block
to return a Tuple(Int32, _) containing the (adjusted) magnitude and unit.
The magnitude is typically adjusted to a multiple of 3.
def humanize_length(number)
number.humanize do |magnitude, number|
case magnitude
when -2, -1 then {-2, " cm"}
when .>=(4)
{3, " km"}
else
magnitude = Number.prefix_index(magnitude)
{magnitude, " #{Number.si_prefix(magnitude)}m"}
end
end
end
humanize_length(1_420) # => "1.42 km"
humanize_length(0.23) # => "23.0 cm"
See Int#humanize_bytes to format a file size.
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 true if self is less than zero.
-1.negative? # => true
0.negative? # => false
1.negative? # => false
Returns true if self is greater than zero.
-1.positive? # => false
0.positive? # => false
1.positive? # => true
Rounds self to an integer value using rounding mode.
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).
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
Returns the sign of this number as an Int32.
-1if this number is negative0if this number is zero1if this number is positive
123.sign # => 1
0.sign # => 0
-42.sign # => -1
Keeps digits significant digits of this number in the given base.
1234.567.significant(1) # => 1000
1234.567.significant(2) # => 1200
1234.567.significant(3) # => 1230
1234.567.significant(4) # => 1235
1234.567.significant(5) # => 1234.6
1234.567.significant(6) # => 1234.57
1234.567.significant(7) # => 1234.567
1234.567.significant(8) # => 1234.567
15.159.significant(1, base = 2) # => 16
Performs a #step in the direction of the limit. For instance:
10.step(to: 5).to_a # => [10, 9, 8, 7, 6, 5]
5.step(to: 10).to_a # => [5, 6, 7, 8, 9, 10]
Performs a #step in the direction of the limit. For instance:
10.step(to: 5).to_a # => [10, 9, 8, 7, 6, 5]
5.step(to: 10).to_a # => [5, 6, 7, 8, 9, 10]
Macros
Creates an Array of self with the given values, which will be casted
to this type with the new method (defined in each Number type).
floats = Float64[1, 2, 3, 4]
floats.class # => Array(Float64)
ints = Int64[1, 2, 3]
ints.class # => Array(Int64)
This is similar to an array literal of the same item type:
Int64[1, 2, 3, 4] # : Array(Int64)
[1, 2, 3, 4] of Int64 # : Array(Int64)
Creates a Slice of self with the given values, which will be casted
to this type with the new method (defined in each Number type).
The slice is allocated on the heap.
floats = Float64.slice(1, 2, 3, 4)
floats.class # => Slice(Float64)
ints = Int64.slice(1, 2, 3)
ints.class # => Slice(Int64)
This is a convenient alternative to Slice.[] for designating a
specific item type which also considers autocasting.
Int64.slice(1, 2, 3, 4) # : Slice(Int64)
Slice[1_i64, 2_i64, 3_i64, 4_i64] # : Slice(Int64)
Creates a StaticArray of self with the given values, which will be casted
to this type with the new method (defined in each Number type).
floats = Float64.static_array(1, 2, 3, 4)
floats.class # => StaticArray(Float64, 4)
ints = Int64.static_array(1, 2, 3)
ints.class # => StaticArray(Int64, 3)
This is a convenvenient alternative to StaticArray.[] for designating a
specific item type which also considers autocasting.
Int64.static_array(1, 2, 3, 4) # : StaticArray(Int64)
StaticArray[1_i64, 2_i64, 3_i64, 4_i64] # : StaticArray(Int64)