Created
February 23, 2012 03:13
-
-
Save dlchet/1889741 to your computer and use it in GitHub Desktop.
float code, commented
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 Float # because we are re-opening class float, anywhere we refer to self in | |
def pretty_s # this method will be the floating point number we call "pretty_s" on. | |
num = "%.12g" % self # num is a string representation of self with 12 decimal places | |
num.sub!(/\.(.*?)0+$/,".$1") # remove any trailing zeroes | |
# might be like 2. at this point # if we had all zeroes, there would be nothing after the decimal point | |
num = num[0..-2] if num[-1] == '.' # thus, let's remove the decimal point in that case | |
num # and return our string | |
end | |
end | |
p 1.1-0.9 # >> 0.20000000000000007 # floating point yuckiness means that we get this number instead of 0.2 | |
p (1.1-0.9).pretty_s # >> "0.2" # our pretty_s method solves that problem | |
p 2.0.pretty_s # >> "2" # here is what happens when there are only zeroes after the decimal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment