In Ruby, numbers are numerical values that can be used for mathematical operations.
Here's a quick example of Ruby numbers. You can read the rest of the tutorial to learn more.
Example
puts 5 # Output: 5
puts 5.55 # Output: 5.55
puts 5 + 5.55 # Output: 10.55
Here, 5 and 5.55 are numbers, and we can perform arithmetic operations on them, such as addition.
Ruby Number Types
The following data types are considered numbers in Ruby:
Data Type | Description | Example |
---|---|---|
Integer | Whole numbers without decimal points. | 5 , -23 , 1024 , etc. |
Float | Numbers with decimal points. | 5.0 , -23.85 , 1024.369 , etc. |
Rational | Rational numbers, i.e., fractions like 2/3 or 4/5 . |
Rational(2, 3) , Rational(4, 5) , etc. |
Complex | Complex numbers with real and imaginary parts, like 2 + 3i . |
Complex(2, 3) , Complex(4, 5) , etc. |
BigDecimal | Very large or very precise decimal numbers. | BigDecimal("0.123456789123456789") |
Here, Integer
and Float
are basic number types while the others are advanced types.
Ruby Integer
An integer is a number without a decimal point like 20, -170, 300, etc. For example,
# Create and print an integer variable
marks = 85
puts marks
# Output: 85
Here, marks
is an integer variable whose value is 85.
Ruby Float
A float is a number with a decimal point like 20.25, -170.39, 300.0, etc. For example,
# Create and print a float variable
price = 99.99
puts price
# Output: 99.99
Here, price
is a float variable whose value is 99.99.
Arithmetic Operations with Numbers
You can use numbers in arithmetic operations. For example,
# Arithmetic Operations on Integers
puts 9 + 3 # Output: 12
puts 9 - 3 # Output: 6
puts 9 * 3 # Output: 27
puts 9 / 3 # Output: 3
puts 9 % 3 # Output: 0
# Arithmetic Operations on Floats
puts 6.9 + 2.5 # Output: 9.4
puts 6.9 - 2.5 # Output: 4.4
puts 6.9 * 2.5 # Output: 17.25
puts 6.9 / 2.5 # Output: 2.7600000000000002
puts 6.9 % 2.5 # Output: 1.9000000000000004
More on Ruby Integers and Floats
Consider the following code:
puts 6.9 / 2.5 # Output: 2.7600000000000002
puts 6.9 % 2.5 # Output: 1.9000000000000004
Notice that:
6.9 / 2.5
gives 2.7600000000000002 instead of 2.76.6.9 % 2.5
gives 1.9000000000000004 instead of 1.9.
This is because computers represent decimals using binary numbers (base-2 numbers).
However, some decimal numbers cannot be represented exactly in binary.
In such cases, Ruby stores an approximation of the number, leading to tiny precision errors during calculations. For example,
puts 0.1 + 0.2
# Output: 0.30000000000000004
While these small differences are usually not a problem, you should be aware of them when:
- Comparing float numbers. For example,
a == b
may returnfalse
even if they look equal. - Working with money or scientific values, in which case you should use
BigDecimal
for precision.
Performing division on two integers will always result in an integer result. For example,
puts 9 / 4
# Output: 2
Here, both 9 and 4 are integers. So 9 / 4
will give us the integer value 2 instead of the actual result, which is 2.25.
To fix this issue, you must use at least one float number in your operation.
Alternatively, you can use the fdiv
method for float division.
Note: fdiv
is only available starting from Ruby 2.4.
Example
# Divide 9.0 (float) by 4 (integer)
puts 9.0 / 4
# Divide 9 (integer) by 4.0 (float)
puts 9 / 4.0
# Divide 9.0 by 4.0
puts 9.0 / 4.0
# Use fdiv method (valid only for Ruby 2.4 and later)
puts 9.fdiv(4)
Output
2.25 2.25 2.25 2.25
Remember:
- Integer ÷ Integer = Integer (drops the decimal)
- Float ÷ anything = Float (keeps the decimal)
Integer division also has another interesting feature. Consider the following program:
puts 7 / 2 # Output: 3
puts -7 / 2 # Output: -4
puts 7 / -2 # Output: -4
puts -7 / -2 # Output: 3
Here,
-7 / 2
and7 / -2
gives -4.7 / 2
and-7 / -2
gives 3.
This is because Ruby always rounds down towards negative infinity when dividing integers. This is known as floor division.
So, we get -4 because it is closer to negative infinity compared to -3. Similarly, 3 is closer to negative infinity than 4.
Ruby Number Methods
Ruby numbers have many handy methods you can use. Some common ones are:
Method | Description |
---|---|
even? |
Checks if the integer is even. |
odd? |
Checks if the integer is odd. |
ceil |
Rounds the float up. |
floor |
Rounds the float down. |
round |
Rounds the float to the nearest integer. |
next |
Next consecutive integer. |
pred |
Previous consecutive integer. |
to_s |
Converts number to string. |
gcd(x) |
Greatest common divisor with x . |
n.times |
Repeats a block of code n times. |
Example: Number Methods
puts 6.even? # true
puts 7.odd? # true
puts 8.3.ceil # 9
puts 3.14.floor # 3
puts 5.next # 6
puts 5.pred # 4
puts 12.gcd(8) # 4
3.times { |num| puts "Number #{num}" }
Output
true true 9 3 6 4 4 Number 0 Number 1 Number 2
Ruby Rational Numbers
Rational numbers are fractions like 2/3
or 4/5
. Unlike floats, they represent exact values without approximations.
Syntax
Rational(numerator, denominator)
Example
num_rational = Rational(2, 3)
puts num_rational
# Output: 2/3
# Convert integer to rational number
puts 3.to_r
# Output: 3/1
# Convert float to rational
puts 0.5.to_r
# Output: 1/2
# Convert numerical string to rational
puts "0.5".to_r
# Output: 1/2
Ruby Complex Numbers
Complex numbers have a real and an imaginary part. For example, in the complex number 2 + 3i
:
- 2 is the real part.
- 3 is the imaginary part since it is indicated by
i
(the symbol for imaginary numbers).
Syntax
Complex(real, imag)
Example
complex1 = Complex(2, 3)
complex2 = Complex(3, 4)
puts complex1 # Output: 2+3i
# Get real and imaginary parts
puts complex1.real # Output: 2
puts complex1.imag # Output: 3
# Add complex numbers
puts complex1 + complex2 # Output: 5+7i
Note: In Ruby versions older than 2.0.0, you may need to add require 'complex'
to load the Complex
library from the standard library. Starting with Ruby 2.0.0, this library is loaded by default, so no additional code is needed.
Ruby BigDecimal
You can use BigDecimal
for very large or very precise decimal numbers. It's useful for financial or scientific calculations where precision matters.
Example
require 'bigdecimal'
num_bd = BigDecimal("0.123456789123456789")
puts num_bd
# Output: 0.123456789123456789e0
Here's how the program works:
Code | Description |
---|---|
require 'bigdecimal' |
Loads the BigDecimal library into your program. You cannot create BigDecimal numbers without loading it. |
BigDecimal("0.123456789123456789") |
Creates a BigDecimal number by accepting a numerical string as its argument. |
Frequently Asked Questions
You can use the .class
method with a number to find its type. For example,
require 'bigdecimal'
puts 5.class
puts 5.5.class
puts Rational(2, 3).class
puts Complex(2, 3).class
puts BigDecimal("0.234598").class
Output
Integer Float Rational Complex BigDecimal
You can use the following methods to convert between number types:
to_i
- Converts to integer.to_f
- Converts to float.to_r
- Converts to rational number.
For example,
# Convert integer to float
puts 5.to_f # Output: 5.0
# Convert float to integer
puts 5.5.to_i # Output: 5
# Convert float to real number
puts 5.5.to_r # Output: 11/2
# Convert rational number to float
puts Rational(11, 2).to_f # Output: 5.5
# Convert rational number to integer
puts Rational(11, 2).to_i # Output: 5
# Convert numerical string to integer
puts "5.5".to_i # Output: 5
# Convert numerical string to float
puts "5.5".to_f # Output: 5.5
# Convert numerical string to rational number
puts "5.5".to_r # Output: 11/2
You can use Ruby's Math
module to access mathematical constants and functions:
puts Math::PI # Output: 3.141592653589793
puts Math.sqrt(16) # Output: 4.0
puts Math.sin(0) # Output: 0.0
In Ruby, BigDecimal
and Complex
are parts of Ruby's standard library, not its core library. Thus, you need to load it manually using the following codes:
# Load BigDecimal library
require 'bigdecimal'
# Load Complex library
require 'complex'
However, many Ruby versions load the Complex
library by default (but not the BigDecimal
library).