Library for converting numbers to words?

I’d like to print “zero reservations”, “one reservation”, “two reservations”, etc. I can use inflex to get plurals and singulars, but I can’t find anything that’s the equivalent of Ruby’s to_words or humanize gems. Neither inflex nor Number has what I want:

to_word(1) # "one"

Am I missing something? Were I to port one of the Ruby libraries, is there an Elixir library to target for a pull request?

1 Like
4 Likes

Thanks for the shoutout. The package on hex is ex_cldr_numbers (I am the author). Some examples from the readme:

Formatting numbers as words, ordinals and roman numerals

  • As words. For example, formatting 123 into “one hundred and twenty-three” for the “en” locale. The applicable format types are :spellout and :spellout_verbose.
iex> MyApp.Cldr.Number.to_string 123, format: :spellout
{:ok, "one hundred twenty-three"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout, locale: "de"
{:ok, "ein­hundert­drei­und­zwanzig"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout, locale: "fr"
{:ok, "cent vingt-trois"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout, locale: "th"
{:ok, "หนึ่ง​ร้อย​ยี่​สิบ​สาม"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout, locale: "he"
{:ok, "מאה עשרים ושלוש"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout, locale: "es"
{:ok, "ciento veintitrés"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout, locale: "zh"
{:ok, "一百二十三"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout_verbose
{:ok, "one hundred and twenty-three"}
  • As a year. In many languages the written form of a year is different to that used for an arbitrary number. For example, formatting 1989 would result in “nineteen eighty-nine”. The applicable format type is :spellout_year.
iex> MyApp.Cldr.Number.to_string 2017, format: :spellout_year
{:ok, "twenty seventeen"}
  • As an ordinal. For example, formatting 123 into “123rd”. The applicable format types are :ordinal, :spellout_ordinal and :spellout_ordinal_verbose.
iex> MyApp.Cldr.Number.to_string 123, format: :ordinal
{:ok, "123rd"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout_ordinal
{:ok, "one hundred twenty-third"}

iex> MyApp.Cldr.Number.to_string 123, format: :spellout_ordinal_verbose
{:ok, "one hundred and twenty-third"}

iex> MyApp.Cldr.Number.to_string 123, format: :ordinal, locale: "fr"
{:ok, "123e"}

iex> MyApp.Cldr.Number.to_string 123, format: :ordinal, locale: "zh"
{:ok, "第123"}
  • As Roman numerals. For example, formatting 123 into “CXXIII”. The applicable formats are :roman or :roman_lower. Note that roman number formatting is only supported for numbers between 1 and 5,000.
iex> MyApp.Cldr.Number.to_string 123, format: :roman
{:ok, "CXXIII"}

iex> MyApp.Cldr.Number.to_string 123, format: :roman_lower
{:ok, "cxxiii"}

iex> MyApp.Cldr.Number.to_string 12345, format: :roman_lower
{:ok, "12,345"}
7 Likes

I keep being impressed by the high quality of your work. Extremely well done.

3 Likes