Ruby:
Rounding numbers

How to:

# Basic rounding
puts 3.14159.round      # => 3
puts 2.6.round          # => 3

# Specifying precision
puts 3.14159.round(2)   # => 3.14
puts 2.675.round(2)     # => 2.68

# Rounding down
puts 2.9.floor          # => 2

# Rounding up
puts 2.1.ceil           # => 3

# Rounding towards zero
puts -2.9.round         # => -3
puts -2.9.truncate      # => -2

Sample Output:

3
3
3.14
2.68
2
3
-3
-2

Deep Dive

Rounding numbers isn’t new—humans have been doing it for centuries to make calculations easier or to work within the limits of their tools. In Ruby, the round method is versatile, with the ability to round to the nearest whole number by default or to a specified decimal place.

An alternative to round is floor for always rounding down, and ceil for always rounding up, regardless of the number’s value. To just chop off the decimal places, you’ve got truncate.

Historically, when it comes to computers, rounding becomes critical in dealing with floating-point arithmetic due to its inherent imprecision. Ruby, like most languages, follows the IEEE 754 standard for floating-point numbers, which means it handles rounding in a way that most programmers should be able to predict and rely on.

There’s more to it, though—things like the banker’s rounding (also known as round half to even) are concepts that Ruby developers may need to manually implement, since the round method doesn’t offer it out of the box.

See Also