Numbers
Converting a String to Integer
Section titled “Converting a String to Integer”You can use the Integer method to convert a String to an Integer:
Integer("123") # => 123Integer("0xFF") # => 255Integer("0b100") # => 4Integer("0555") # => 365You can also pass a base parameter to the Integer method to convert numbers from a certain base
Integer('10', 5) # => 5Integer('74', 8) # => 60Integer('NUM', 36) # => 30910Note that the method raises an ArgumentError if the parameter cannot be converted:
Integer("hello")# raises ArgumentError: invalid value for Integer(): "hello"Integer("23-hello")# raises ArgumentError: invalid value for Integer(): "23-hello"You can also use the String#to_i method. However, this method is slightly more permissive and has a different behavior than Integer:
"23".to_i # => 23"23-hello".to_i # => 23"hello".to_i # => 0String#to_i accepts an argument, the base to interpret the number as:
"10".to_i(2) # => 2"10".to_i(3) # => 3"A".to_i(16) # => 10Creating an Integer
Section titled “Creating an Integer”0 # creates the Fixnum 0123 # creates the Fixnum 1231_000 # creates the Fixnum 1000. You can use _ as separator for readabilityBy default the notation is base 10. However, there are some other built-in notations for different bases:
0xFF # Hexadecimal representation of 255, starts with a 0x0b100 # Binary representation of 4, starts with a 0b0555 # Octal representation of 365, starts with a 0 and digitsRounding Numbers
Section titled “Rounding Numbers”The round method will round a number up if the first digit after its decimal place is 5 or higher and round down if that digit is 4 or lower. This takes in an optional argument for the precision you’re looking for.
4.89.round # => 54.25.round # => 43.141526.round(1) # => 3.13.141526.round(2) # => 3.143.141526.round(4) # => 3.1415Floating point numbers can also be rounded down to the highest integer lower than the number with the floor method
4.9999999999999.floor # => 4They can also be rounded up to the lowest integer higher than the number using the ceil method
4.0000000000001.ceil # => 5Even and Odd Numbers
Section titled “Even and Odd Numbers”The even? method can be used to determine if a number is even
4.even? # => true5.even? # => falseThe odd? method can be used to determine if a number is odd
4.odd? # => false5.odd? # => trueConverting a number to a string
Section titled “Converting a number to a string”Fixnum#to_s takes an optional base argument and represents the given number in that base:
2.to_s(2) # => "10"3.to_s(2) # => "11"3.to_s(3) # => "10"10.to_s(16) # => "a"If no argument is provided, then it represents the number in base 10
2.to_s # => "2"10423.to_s # => "10423"Rational Numbers
Section titled “Rational Numbers”Rational represents a rational number as numerator and denominator:
r1 = Rational(2, 3)r2 = 2.5.to_rr3 = r1 + r2r3.numerator # => 19r3.denominator # => 6Rational(2, 4) # => (1/2)Other ways of creating a Rational
Rational('2/3') # => (2/3)Rational(3) # => (3/1)Rational(3, -5) # => (-3/5)Rational(0.2) # => (3602879701896397/18014398509481984)Rational('0.2') # => (1/5)0.2.to_r # => (3602879701896397/18014398509481984)0.2.rationalize # => (1/5)'1/4'.to_r # => (1/4)Complex Numbers
Section titled “Complex Numbers”1i # => (0+1i)1.to_c # => (1+0i)rectangular = Complex(2, 3) # => (2+3i)polar = Complex('1@2') # => (-0.4161468365471424+0.9092974268256817i)
polar.rectangular # => [-0.4161468365471424, 0.9092974268256817]rectangular.polar # => [3.605551275463989, 0.982793723247329]rectangular + polar # => (1.5838531634528576+3.909297426825682i)Dividing two numbers
Section titled “Dividing two numbers”When dividing two numbers pay attention to the type you want in return. Note that dividing two integers will invoke the integer division. If your goal is to run the float division, at least one of the parameters should be of float type.
Integer division:
3 / 2 # => 1Float division
3 / 3.0 # => 1.0
16 / 2 / 2 # => 416 / 2 / 2.0 # => 4.016 / 2.0 / 2 # => 4.016.0 / 2 / 2 # => 4.0Remarks
Section titled “Remarks”Numbers hierarchy
Section titled “Numbers hierarchy”Ruby includes several built-in classes to represent numbers:
Numeric Integer Fixnum # 1 Bignum # 10000000000000000000 Float # 1.0 Complex # (1+0i) Rational # Rational(2, 3) == 2/3 BigDecimal # not loaded by defaultThe most common are:
Fixnumto represent, for instance positive and negative integersFloatto represent floating point numbers
BigDecimal is the only one not loaded by default. You can load it with:
require "bigdecimal"Note that in ruby 2.4+, Fixnum and Bignum are unified; all integers are now just members of the Integer class. For backwards compatibility, Fixnum == Bignum == Integer.