Skip to content

Instantly share code, notes, and snippets.

@chsh
Created February 26, 2011 15:19
Show Gist options
  • Save chsh/845292 to your computer and use it in GitHub Desktop.
Save chsh/845292 to your computer and use it in GitHub Desktop.
Number formatting with columns. You should have better implementation. :-P
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