Created
August 30, 2015 05:00
-
-
Save adellhk/79c42c429613bfca481b to your computer and use it in GitHub Desktop.
This file contains hidden or 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(num) | |
separated_num = num.to_s.split("").reverse | |
result = [] | |
separated_num.each_with_index.map do |num, index| | |
if index % 3 == 0 && index != 0 | |
result << num + "," | |
else | |
result << num | |
end | |
end | |
result.reverse.join("") | |
end | |
// can ommit the need to declare result if you use each_with_index.map, but I think this form is simpler. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment