Skip to content

Instantly share code, notes, and snippets.

@jjb
Created March 20, 2013 02:31
Show Gist options
  • Select an option

  • Save jjb/5201853 to your computer and use it in GitHub Desktop.

Select an option

Save jjb/5201853 to your computer and use it in GitHub Desktop.
def divisible_by_3?(number)
number%3 == 0
end
def commaifier(number)
reversed = number.to_s.reverse
result = ""
number.to_s.size.times do |i|
result << reversed[i]
result << "," if divisible_by_3?(i+1)
end
result.reverse
end
# > commaifier(25164150)
# => "25,164,150"
# > commaifier(1234567890)
# => "1,234,567,890"
@jjb

jjb commented Mar 20, 2013

Copy link
Copy Markdown
Author

yours is shorter than mine...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment