Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round up to the nearest tenth?

I need to round up to the nearest tenth. What I need is ceil but with precision to the first decimal place.

Examples:

10.38 would be 10.4
10.31 would be 10.4
10.4 would be 10.4

So if it is any amount past a full tenth, it should be rounded up.

I'm running Ruby 1.8.7.

like image 593
Shpigford Avatar asked Aug 29 '11 16:08

Shpigford


2 Answers

This works in general:

ceil(number*10)/10

So in Ruby it should be like:

(number*10).ceil/10.0
like image 148
Gumbo Avatar answered Oct 12 '22 06:10

Gumbo


Ruby's round method can consume precisions:

10.38.round(1) # => 10.4

In this case 1 gets you rounding to the nearest tenth.

like image 36
ktusznio Avatar answered Oct 12 '22 04:10

ktusznio