Skip to content

Instantly share code, notes, and snippets.

@Rojo
Created March 13, 2014 06:24
Show Gist options
  • Save Rojo/9522788 to your computer and use it in GitHub Desktop.
Save Rojo/9522788 to your computer and use it in GitHub Desktop.
Label the magnitudes of a number.
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