[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rounding to the nearest 0.05

Peter Roome

10/26/2007 7:20:00 PM

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
please?

Kind Regards

Pete
--
Posted via http://www.ruby-....

14 Answers

Phrogz

10/26/2007 7:32:00 PM

0

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
> please?

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

Coey Minear

10/26/2007 7:36:00 PM

0

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
> please?
>
> Kind Regards
>
> Pete
> --
> Posted via http://www.ruby-....
>

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


Peter Roome

10/26/2007 7:52:00 PM

0

> 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-....

Konrad Meyer

10/26/2007 8:07:00 PM

0

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-....

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.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Peter Roome

10/26/2007 8:10:00 PM

0

Konrad Meyer wrote:
> Quoth Peter Roome:
>> would the .to_f make between these solutions?
>> --
>> Posted via http://www.ruby-....
>
> 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-....

Peter Roome

10/26/2007 8:48:00 PM

0


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-....

Phrogz

10/26/2007 9:01:00 PM

0

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

Peter Roome

10/26/2007 9:15:00 PM

0

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-....

Andrew McGrath

7/29/2013 11:28:00 AM

0

Adam H. Kerman wrote:
> AC <xxx@xxx.xxx> wrote:
>> Adam H. Kerman wrote:
>
>>> What's with reviving a dead thread?
>
>> Ha! New PC, so new install of newsreader. Was just adding newsgroups,
>> and this popped up. It was interesting. I read the books that covered
>> what happened to Anna in detail, thought I'd share.
>
> Good timing. Thank you.
>

No problem.

If you can, the books are so worth reading, especially the ones that
cover the Shadows, Anna, Morden, Galen, and the Technomages. (Granted
some of the stand alone books are a bit crap.) For me, its a real shame
these stories were never on screen. The trilogies would have made decent
mini series'.

--
AC

Andrew McGrath

7/29/2013 11:29:00 AM

0

Merrick Baldelli wrote:

>
>> So obviously you saw no chemistry, or any indication they were one lovers.
>
> Your notion is certainly romantic, but even the way the scene
> was written, Sheridan was being a pragmatist wanting to make sure he
> wasn't allowing his feelings were being manipulated.
>

How is that romantic?

--
AC