Pls help me regarding Maths round up function


#1

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


#2

Hi.

(value*100).round/100.0

Stan


#3

On 6/6/06, Prashant T. removed_email_address@domain.invalid 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


#4

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


#5

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