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
On 6/6/06, Prashant T. [email protected] wrote:
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
Is there any function???
puts “%.2f” % 223.3234235235 # =>223.32
Hey 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
Prashant T. 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
The 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