Add commas to a big integer value
def commas(x)
str = x.to_s.reverse
str.gsub!("([0-9]{3})","\\1,")
str.gsub(",$","").reverse
end
puts commas(123) # "123"
puts commas(1234) # "1,234"
puts commas(12345) # "12,435"
puts commas(123456) # "123,456"
puts commas(1234567) # "1,234,567"
Related examples in the same category