Friday, January 18, 2008

How to round a float number after the decimal point in Ruby

This the best way I found to do this in Ruby:

It's using a shorter way of sprintf.


>> i = 5.546
=> 5.546
>> ("%.2f" % i).to_f
=> 5.55


the .2 says 2 points after the decimal point.

If you want you can wrap it up in method and extend the Float class.

1 comments:

Hcg weight loss said...

Wow, nice post,there are many person searching about that now they will find enough resources by your post.Thank you for sharing to us.Please one more post about that..

:popular_tags => [ruby, rails, ruby-on-rails, רובי-און-ריילס, console,,tricks, youtube, links, screeshots, toturials],
:email_me => 'shmuel@ahdut.com',
:subscribe_to_rss => ,
:sites => [pawst.com, urlazy.com],
:sponsored_by =>