Created
October 26, 2010 05:01
-
-
Save chrisbnt/646357 to your computer and use it in GitHub Desktop.
Nuumeric#round_to_nearest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Numeric | |
def round_to_nearest(delta = 0.25) | |
return self if delta.to_f == 0.0 | |
((to_f / delta.to_f).round * delta.to_f) | |
end | |
def floor_to_nearest(delta = 0.25) | |
return self if delta.to_f == 0.0 | |
((to_f / delta.to_f).floor * delta.to_f) | |
end | |
def ceil_to_nearest(delta = 0.25) | |
return self if delta.to_f == 0.0 | |
((to_f / delta.to_f).ceil * delta.to_f) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment