All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

Integer division in Ruby: why 6 / 4 equals 1

Post cover: Integer division in Ruby: why 6 / 4 equals 1
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In Ruby, when you divide two integers (Integer), the result will also be an integer. Therefore:
6 / 4
returns 1, not 1.5. Ruby simply truncates the fractional part - this is called integer division.

Why this happens

  • 6 and 4 are both objects of class Integer.
  • The / operator between two integers returns Integer, cutting off the decimal part.
6.class   # => Integer
4.class   # => Integer
6 / 4     # => 1

How to get the correct result with a decimal part

There are several options. Let's consider them one by one.
How to get the correct result with a decimal part:
6.0 / 4   # => 1.5
6 / 4.0   # => 1.5
Use the fdiv method. The fdiv method performs division and always returns Float, even if both numbers are integers. This is convenient when you need precise fractional division without explicit type conversion.
6.fdiv(4) # => 1.5
Explicit type conversion (to_f). The to_f method converts a number to Float. After that, division is automatically performed as with floating-point numbers, and the result will also be Float. You can also convert the divisor.
6.to_f / 4   # => 1.5
In short:
Integer division (Integer / Integer) truncates the fractional part.
To get a floating-point number (Float), you need to:
  • use Float in the division (6.0 / 4)
  • or the fdiv method (6.fdiv(4))
Here are a few more examples:
# Integer division
p 7 / 2       # => 3

# Division with Float
p 7.0 / 2     # => 3.5
p 7 / 2.0     # => 3.5
p 7.fdiv(2)   # => 3.5
p 7.to_f / 2  # => 3.5

This post doesn't have any additions from the author yet.

How arrays work in Ruby: practical examples of each, map, select, inject, reduce, filter_map
28 Oct 10:38

How arrays work in Ruby: practical examples of each, map, select, inject, reduce, filter_map

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
How self, protected, and private work (Ruby)
28 Oct 13:52

How self, protected, and private work (Ruby)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
28 Oct 14:42

How &:to_s works in Ruby and what Symbol#to_proc is

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What are Proc and Lambda in Ruby?
28 Oct 15:57

What are Proc and Lambda in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What happens if you call [1, 2, 3].map(&Person)
29 Oct 17:54

What happens if you call [1, 2, 3].map(&Person)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Singleton class (eigenclass) in Ruby: what it is and why it is needed
29 Oct 18:29

Singleton class (eigenclass) in Ruby: what it is and why it is needed

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
==, equal?, eql?, === in Ruby: what they check and when to use them
29 Oct 20:47

==, equal?, eql?, === in Ruby: what they check and when to use them

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Include, Extend, Prepend in Ruby: how they work and what the difference is
29 Oct 21:20

Include, Extend, Prepend in Ruby: how they work and what the difference is

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska