Created
March 13, 2014 06:24
-
-
Save Rojo/9522788 to your computer and use it in GitHub Desktop.
Label the magnitudes of a number.
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 labelize number | |
magnitudes = [ | |
[1e12.to_i, 'trillion'], [1e9.to_i, 'billion'], [1e6.to_i, 'million'], | |
[1e3.to_i, 'thousand'], [1e2.to_i, 'hundred'] | |
] | |
left = number | |
magnitudes.each do | label | | |
if left / label[0] > 0 | |
write = left / label[0] | |
left = left - write * label[0] | |
puts write, label[1], left | |
end | |
end | |
end | |
labelize 999999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment