Created
August 15, 2013 05:40
-
-
Save joshmcarthur/6238536 to your computer and use it in GitHub Desktop.
Swedish rounding (.45 up to .5) for Floats
This file contains hidden or 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 Float | |
# Public: Round a decimal number using the Swedish rounding system. | |
# | |
# This method complements Float#round but is intended for dealing with | |
# currencies, etc. where a more 'real-life' rounding technique is needed. | |
# | |
# Basically, the vanilla Float#round method only looks at the first number, | |
# which can lead to bad rounds. | |
# | |
# Example | |
# | |
# 4.45.round | |
# # => 4 | |
# | |
# This method rounds more 'right-to-left' - that is, it yields the number | |
# you would expect to gain from rounding this number if it was, for example | |
# currency. | |
# | |
# Examples | |
# | |
# 4.45.round_swedish | |
# # => 5 | |
# 4.44.round_swedish | |
# # => 4 | |
# | |
# Returns an integer | |
def round_swedish | |
((self * 10.0).round / 10.0).round | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment