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.

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.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement