Last active
September 6, 2016 20:26
-
-
Save marcinlerka/c4a5bce000b16e68ec398826211fc889 to your computer and use it in GitHub Desktop.
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
# http://play.codesthq.com/game#1 | |
# Write a method called format_number which converts given integer number to string like this: | |
# format_number(49) #=> '49' | |
# format_number(12345) #=> '12_345' | |
# format_number(-134567) #=> '-134_567' | |
# format_number(1234567890) #=> '1_234_567_890' | |
# The idea is to create groups of 3 digits separated by '_' character. | |
def format_number(number) | |
number_str = number.to_s.split('') | |
result = [] | |
until number_str.empty? do | |
result.unshift(number_str.pop(3).join()) | |
end | |
if result[0] == "-" | |
result[1].prepend(result.shift); | |
end | |
result.join("_") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment