Skip to content

Instantly share code, notes, and snippets.

@sidazhang
Created February 20, 2013 11:49
Show Gist options
  • Save sidazhang/4994984 to your computer and use it in GitHub Desktop.
Save sidazhang/4994984 to your computer and use it in GitHub Desktop.
separate numbers with commas
def separate_comma(number)
results = number.to_s.split("")
reverse_counter = results.length - 1
output = ""
comma_counter = 0
results.length.times do |i|
output = results[reverse_counter] + output
reverse_counter = reverse_counter - 1
comma_counter = comma_counter + 1
if comma_counter%3 == 0 && comma_counter/number.to_s.length != 1
output = "," + output
else
end
end
output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment