Created
March 1, 2013 08:44
-
-
Save vpivo/5063318 to your computer and use it in GitHub Desktop.
Seperate Numbers with Commas
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
def separate_comma(number) | |
if number < 1000 | |
puts "silly" | |
end | |
snumb = number.to_s | |
array = snumb.split('') | |
cstr = '' | |
counter = 0 | |
array.reverse.each do |i| | |
cstr += i | |
counter += 1 | |
if counter == 3 | |
cstr += ',' | |
counter = 0 | |
end | |
end | |
cstr = cstr.reverse | |
if cstr[0] == ',' | |
cstr.slice!(0,1) | |
end | |
puts cstr | |
end | |
separate_comma(100000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment