for example:
i = 0.33533333333 round up to “0.34”
thanks.
for example:
i = 0.33533333333 round up to “0.34”
thanks.
On Tue, 2007-01-16 at 03:54 -0800, Michael W. wrote:
david wrote:
i = 0.33533333333 round up to “0.34”
sprintf("%2.2f", i)
Hmm and if you want a float then:
eval(sprintf("%2.2f",i))
Also, if you want to just round:
i.round #=> will give 0 for 0.33335
And If you don’t really like eval then, you would have to get your hand
dirty and :
class Float
def round2f places
sprintf("%.#{places}f",self).to_f
end
end
david wrote:
i = 0.33533333333 round up to “0.34”
sprintf("%2.2f", i)
–
Michael W.
hi,
what about this…
irb(main):035:0> i = 0.33533333333
=> 0.33533333333
irb(main):036:0> Integer(i*100)/100.0
=> 0.33
in case .34 is required
can do
irb(main):040:0> Integer((i*100).ceil)
=> 34
regards
gaurav
Hi, I’ve got it. thanks, everyone.
On 1 16 , 10 53 , “gaurav bagga”
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs