Skip to content

Instantly share code, notes, and snippets.

@begriffs
Created January 29, 2012 17:47
Show Gist options
  • Save begriffs/1699809 to your computer and use it in GitHub Desktop.
Save begriffs/1699809 to your computer and use it in GitHub Desktop.
English words for numbers
def word(n)
divisors = {
:vigintillion => 10**63, :novemdecillion => 10**60,
:octodecillion => 10**57, :septendecillion => 10**54,
:sexdecillion => 10**51, :quindecillion => 10**48,
:quattuordecillion => 10**45, :tredecillion => 10**42,
:duodecillion => 10**39, :undecillion => 10**36,
:decillion => 10**33, :nonillion => 10**30,
:octillion => 10**27, :septillion => 10**24,
:sextillion => 10**21, :quintillion => 10**18,
:quadrillion => 10**15, :trillion => 10**12,
:billion => 10**9, :million => 10**6,
:thousand => 10**3, :hundred => 100
}
subtrahends = {
:ninety => 90, :eighty => 80,
:seventy => 70, :sixty => 60,
:fifty => 50, :forty => 40,
:thirty => 30, :twenty => 20,
:nineteen => 19, :eighteen => 18,
:seventeen => 17, :sixteen => 16,
:fifteen => 15, :fourteen => 14,
:thirteen => 13, :twelve => 12,
:eleven => 11, :ten => 10,
:nine => 9, :eight => 8,
:seven => 7, :six => 6,
:five => 5, :four => 4,
:three => 3, :two => 2,
:one => 1
}
return "negative " + word(-n) if n < 0
return '' if n == 0
divisors.each do |name, value|
if n >= value
return word(n/value) + "#{name} " + word(n % value)
end
end
subtrahends.each do |name, value|
if n >= value
return "#{name} " + word(n - value)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment