Skip to content

Instantly share code, notes, and snippets.

@chrisbnt
Created October 26, 2010 05:01
Show Gist options
  • Save chrisbnt/646357 to your computer and use it in GitHub Desktop.
Save chrisbnt/646357 to your computer and use it in GitHub Desktop.
Nuumeric#round_to_nearest
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