Created
February 26, 2011 15:19
-
-
Save chsh/845292 to your computer and use it in GitHub Desktop.
Number formatting with columns. You should have better implementation. :-P
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 Integer | |
def to_cs(numcol = 3) | |
unit = 10 ** numcol | |
reps = '\d' * numcol | |
neg = false | |
str = self.to_s | |
if self <= -unit | |
neg = true | |
str = (-self).to_s | |
elsif self < unit | |
return str | |
end | |
str = str.reverse.gsub(/(#{reps})/, '\1,').gsub(/,$/, '').reverse | |
str = '-' + str if neg | |
str | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment