Comments on: Rounding to the nearest number in Ruby http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/ Sat, 05 Feb 2011 19:38:40 +0000 hourly 1 https://wordpress.org/?v=5.6 By: Ben http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/comment-page-1/#comment-18291 Sat, 05 Feb 2011 19:38:40 +0000 /2008/01/31/rounding-to-the-nearest-number-in-ruby#comment-18291 Thanks for this, exactly what I needed to round an int up to nearest 500 🙂

]]>
By: Henrik N http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/comment-page-1/#comment-1031 Thu, 03 Sep 2009 12:22:37 +0000 /2008/01/31/rounding-to-the-nearest-number-in-ruby#comment-1031 Tim, I think for rounding to nearest, you’ll find this is easier 🙂


def roundnearest(multiple=10)
  (self/multiple.to_f).round * multiple
end
]]>
By: Tim Harrison http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/comment-page-1/#comment-192 Thu, 31 Jan 2008 17:29:00 +0000 /2008/01/31/rounding-to-the-nearest-number-in-ruby#comment-192 How about this addition?

def roundnearest(nearest=10)
up = roundup(nearest)
down = rounddown(nearest)
if (up-self) < (self-down)
return up
else
return down
end
end

]]>
By: charlie http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/comment-page-1/#comment-193 Thu, 31 Jan 2008 17:29:00 +0000 /2008/01/31/rounding-to-the-nearest-number-in-ruby#comment-193 @Tim – great addition, thanks for posting it 🙂

]]>
By: Hamoth http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/comment-page-1/#comment-194 Thu, 31 Jan 2008 17:29:00 +0000 /2008/01/31/rounding-to-the-nearest-number-in-ruby#comment-194 What is modulus and where are you putting this code?

]]>
By: charlie http://pullmonkey.com/2008/01/31/rounding-to-the-nearest-number-in-ruby/comment-page-1/#comment-195 Thu, 31 Jan 2008 17:29:00 +0000 /2008/01/31/rounding-to-the-nearest-number-in-ruby#comment-195 @Hamoth – modulus returns the remainder of a division. For example if I have modulus(5/2) I will get 1. modulus(10/7), I will get 3. In rails, this code would go in its own file under RAILS_ROOT/config/initializers/. Otherwise, you would just put this code in its own file and require it in your other source code.

]]>