marick

marick

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?

Marked As Solved

kip

kip

ex_cldr Core Team

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"}
18
Post #3

Also Liked

dimitarvp

dimitarvp

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

aenglisc

aenglisc

kip

kip

ex_cldr Core Team

@joaquinalcerro, not all locales have the same RBNF (rules based number formats) rules. That’s why it’s important to check first what rules are available. For :es:

iex> Cldr.Rbnf.rule_names_for_locale :en
{:ok,
 [:spellout_ordinal_verbose, :spellout_numbering_verbose,
  :spellout_cardinal_verbose, :spellout_cardinal, :spellout_ordinal,
  :spellout_numbering_year, :spellout_numbering, :digits_ordinal]}

As you can see there is no :spellout_cardinal but there are :spellout_cardinal_masculine and :spellout_cardinal_feminine. Lets try those:

iex> MyApp.Cldr.Number.to_string 123.456, format: :spellout_cardinal_masculine, locale: :es
{:ok, "ciento veintitrés punto cuatro cinco seis"}
iex> MyApp.Cldr.Number.to_string 123.456, format: :spellout_cardinal_feminine, locale: :es
{:ok, "ciento veintitrés punto cuatro cinco seis"}

Note: Requires the latest ex_cldr_numbers version 2.32.2.

kip

kip

ex_cldr Core Team

Yes. ex_cldr and friends support integers, floats and Decimals.

iex> MyApp.Cldr.Number.to_string Decimal.new(123), format: :spellout
{:ok, "one hundred twenty-three"}
kip

kip

ex_cldr Core Team

Rules based number formats are curious things and the default :spellout_numbering rule (which is what you get with format: :spellout doesn’t support fractional digits. I suspect that it’s because most (maybe all?) locales support :spellout_numbering but not all locales have the notion of spelling out the fractional part.

However all is not lost, there are other rule sets. You can see what rule sets exist for a given locale with:

iex> Cldr.Rbnf.rule_names_for_locale :en
{:ok,
 [:spellout_ordinal_verbose, :spellout_cardinal_verbose, :spellout_cardinal,
  :spellout_ordinal, :spellout_numbering_year, :spellout_numbering_verbose,
  :spellout_numbering, :digits_ordinal]}

And if you try :spellout_cardinal and :spellout_cardinal_verbose you’ll see:

iex> MyApp.Cldr.Number.to_string 1234.456, format: :spellout_cardinal
{:ok, "one thousand two hundred thirty-four point four five six"}

iex> MyApp.Cldr.Number.to_string 1234.456, format: :spellout_cardinal_verbose
{:ok, "one thousand two hundred and thirty-four point four five six"}

Where Next?

Popular in Questions Top

ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement