# Rounding to the nearest 0.05

Hey guys.

Having difficulty figuring out where to begin with trying to round a
calculation to the nearest 0.05. Can anyone offer any suggestions

Kind Regards

Pete

···

--
Posted via http://www.ruby-forum.com/.

irb(main):016:0> 10.times{
irb(main):017:1* n = rand
irb(main):018:1> rounded = (n * 20).round / 20.0
irb(main):019:1> puts "%.02f" % rounded
irb(main):020:1> }
0.95
0.05
0.70
0.50
0.30
0.35
0.95
0.25
0.75
0.45

···

On Oct 26, 1:19 pm, Peter Roome <pete.ro...@googlemail.com> wrote:

Having difficulty figuring out where to begin with trying to round a
calculation to the nearest 0.05. Can anyone offer any suggestions

Peter Roome writes:
> Hey guys.
>
> Having difficulty figuring out where to begin with trying to round a
> calculation to the nearest 0.05. Can anyone offer any suggestions
>
> Kind Regards
>
> Pete
> --
> Posted via http://www.ruby-forum.com/.
>

Just off-hand, my initial reaction would be:

(a * 20).round.to_f/20

where "a" is your variable. Some quick tests with irb seems to show
this as working.

Coey

Just off-hand, my initial reaction would be:

(a * 20).round.to_f/20

where "a" is your variable. Some quick tests with irb seems to show
this as working.

Coey

Cheers for the quick response guys, much appreciated. What difference
would the .to_f make between these solutions?

···

--
Posted via http://www.ruby-forum.com/.

Quoth Peter Roome:

> Just off-hand, my initial reaction would be:
>
> (a * 20).round.to_f/20
>
> where "a" is your variable. Some quick tests with irb seems to show
> this as working.
>
> Coey

Cheers for the quick response guys, much appreciated. What difference
would the .to_f make between these solutions?
--
Posted via http://www.ruby-forum.com/.

One solution used (expr).to_f/20, the other used (expr)/20.0.
Both of these ensure floating point division is used instead of integer
division, that's all.

···

--

Quoth Peter Roome:

would the .to_f make between these solutions?
--
Posted via http://www.ruby-forum.com/.

One solution used (expr).to_f/20, the other used (expr)/20.0.
Both of these ensure floating point division is used instead of integer
division, that's all.

Ahhhh ok thanks for clearing that up!

···

--
Posted via http://www.ruby-forum.com/.

Just out of interest how did you get 20 as the number to multiply and
divide by?! Sorry its been a heavy week :-s

···

--
Posted via http://www.ruby-forum.com/.

1 / 0.05 #=> 20

···

On Oct 26, 2:48 pm, Zoltar Speaks <pete.ro...@googlemail.com> wrote:

Just out of interest how did you get 20 as the number to multiply and
divide by?! Sorry its been a heavy week :-s

Gavin Kistner wrote:

···

On Oct 26, 2:48 pm, Zoltar Speaks <pete.ro...@googlemail.com> wrote:

Just out of interest how did you get 20 as the number to multiply and
divide by?! Sorry its been a heavy week :-s

1 / 0.05 #=> 20

Ace cheers!

--
Posted via http://www.ruby-forum.com/.