Skip to content

Instantly share code, notes, and snippets.

@ibarroladt
Last active December 19, 2015 09:39
Show Gist options
  • Save ibarroladt/5934611 to your computer and use it in GitHub Desktop.
Save ibarroladt/5934611 to your computer and use it in GitHub Desktop.
def separate_comma(number)
s = number.to_s
array = []
s.length.times do |i|
array << s[i].chr
end
array_r = array.reverse
array_r.length.times do |i|
unless (i+1) == array_r.length
if (i+1) % 3 == 0
array_r[i] = "," + array_r[i]
end
end
end
array_r.reverse.to_s
end
puts num = rand(rand(100000000000))*rand(rand(1000000000000))
puts separate_comma(num)
puts separate_comma(num).class
puts
puts num1 = rand(999)
puts a = separate_comma(num1)
puts true if /^\d{1,3}$/.match(a)
puts
puts num2 = rand(999999-1000)+1000
puts b = separate_comma(num2)
puts true if /^\d{1,3},\d{3}$/.match(b)
puts
puts num3 = rand(999999999-1000000)+1000000
puts c = separate_comma(num3)
puts true if /^\d{1,3},\d{3},\d{3}$/.match(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment