Prashant Tiwari
2006-Jun-06 08:04 UTC
[Rails] pls help me regarding Maths round up function.....
Hi, I have some values on my webpage displaying like 1.22333333333 2.33333344444 2.33377777777 etc. Here I want to display values upto 2 decimal places correct. i.e, 1.22333333333 should be dislayed as 1.22 2.33333344444 should be dislayed as 2.33 2.33777777777 should be dislayed as 2.34 & so on.... How to do this in ruby????? Is there any function??? Thanx in advance. Prash -- Posted via http://www.ruby-forum.com/.
スタン マズレック
2006-Jun-06 08:12 UTC
[Rails] pls help me regarding Maths round up function.....
Hi. (value*100).round/100.0 Stan Prashant Tiwari wrote:> Hi, > > I have some values on my webpage displaying like > 1.22333333333 > 2.33333344444 > 2.33377777777 > etc. > Here I want to display values upto 2 decimal places correct. > i.e, 1.22333333333 should be dislayed as 1.22 > 2.33333344444 should be dislayed as 2.33 > 2.33777777777 should be dislayed as 2.34 > & so on.... > > How to do this in ruby????? > > Is there any function??? > > Thanx in advance. > Prash > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060606/fe2e2da1/attachment.html
David Balmain
2006-Jun-06 14:50 UTC
[Rails] pls help me regarding Maths round up function.....
On 6/6/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote:> Hi, > > I have some values on my webpage displaying like > 1.22333333333 > 2.33333344444 > 2.33377777777 > etc. > Here I want to display values upto 2 decimal places correct. > i.e, 1.22333333333 should be dislayed as 1.22 > 2.33333344444 should be dislayed as 2.33 > 2.33777777777 should be dislayed as 2.34 > & so on.... > > How to do this in ruby????? > > Is there any function???puts "%.2f" % 223.3234235235 # =>223.32
Jodi Showers
2006-Jun-07 09:01 UTC
[Rails] pls help me regarding Maths round up function.....
>> >> Is there any function??? > > puts "%.2f" % 223.3234235235 # =>223.32Hey Prash, this will round to a specific number of decimal pts: def round_float(float_num,dec) (float_num*(10**dec)).round.to_f / (10**dec).to_f end round_float(1.238, 2) => 1.24 cheers, Jodi
Alex Wayne
2006-Jun-07 09:04 UTC
[Rails] Re: pls help me regarding Maths round up function.....
Prashant Tiwari wrote:> Hi, > > I have some values on my webpage displaying like > 1.22333333333 > 2.33333344444 > 2.33377777777 > etc. > Here I want to display values upto 2 decimal places correct. > i.e, 1.22333333333 should be dislayed as 1.22 > 2.33333344444 should be dislayed as 2.33 > 2.33777777777 should be dislayed as 2.34 > & so on.... > > How to do this in ruby????? > > Is there any function??? > > Thanx in advance. > PrashThe most elegant way is facets/round_at Check out http://facets.rubyforge.org/doc/api/ Specifically http://facets.rubyforge.org/doc/api/classes/Float.html#M000201 require facet/round_at 4.55555.round_at(2) #=> 4.56 -- Posted via http://www.ruby-forum.com/.