Forum: Ruby on Rails how to round up a float number?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
43cfaf783a5873aace7d365461f51eb4?d=identicon&s=25 david (Guest)
on 2007-01-16 12:41
(Received via mailing list)
for example:

i = 0.33533333333 round up to "0.34"

thanks.
Dfd6f9989147955682701da8227df8b4?d=identicon&s=25 Michael Wang (Guest)
on 2007-01-16 12:56
(Received via mailing list)
david wrote:
> i = 0.33533333333 round up to "0.34"

sprintf("%2.2f", i)

--
Michael Wang
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 Hemant Kumar (gnufied)
on 2007-01-16 14:42
(Received via mailing list)
On Tue, 2007-01-16 at 03:54 -0800, Michael Wang 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
Cd1de7e6a1ff7903b249edc2a4a35b34?d=identicon&s=25 gaurav bagga (Guest)
on 2007-01-16 14:58
(Received via mailing list)
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
43cfaf783a5873aace7d365461f51eb4?d=identicon&s=25 david (Guest)
on 2007-01-17 01:56
(Received via mailing list)
Hi, I've got it. thanks, everyone.

On 1 16 ,   10 53 , "gaurav bagga"
This topic is locked and can not be replied to.