Menu

Toshimaru's Blog

Rounding in Ruby

Rounding off

1.4.round
# => 1
1.5.round
# => 2
-1.5.round
# => -2
-1.4.round
# => -1

Rounding up

1.4.ceil
# => 2
1.5.ceil
# => 2
-1.4.ceil
# => -1
-1.5.ceil
# => -1

Rounding down

1.4.floor
# => 1
1.5.floor
# => 1
-1.5.floor
# => -2
-1.4.floor
# => -2

Rounding down (truncate)

1.4.truncate
# => 1
1.5.truncate
# => 1
-1.5.truncate
# => -1
-1.4.truncate
# => -1

Reference

Load more