Created
August 16, 2008 09:29
-
-
Save ELLIOTTCABLE/5705 to your computer and use it in GitHub Desktop.
Array#to_i - turn a comma-delimited number into an integer
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 Array | |
def to_i | |
i = 0 | |
numeral = 0 | |
self.reverse.inject(0) do |int, section| | |
i += 1 | |
int += section * (10 ** numeral) | |
numeral += section.to_i.to_s.size | |
int | |
end | |
end | |
end | |
[11,694,331.42].to_i #=> 11694331.42 | |
[999,999,999,999,999].to_i #=> 999999999999999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment