numbers_and_words
Spell out numbers in several languages using the I18n gem.
Перевод чисел в слова при помощи библиотеки I18n.
Convertit les nombres en lettres en utilisant la librairie I18n.
Számok betűvel írva az I18n könyvtár segítségével.
I18n kütüphanesi ile sayıları yazıya çevirir.
Soletra números em vários idiomas utilizando a biblioteca I18n.
Deletrea números en varios idiomas utilizando la gema I18n.
Installation / Установка / Installation
gem install numbers_and_wordsOr add it to your Gemfile:
gem 'numbers_and_words'Usage / Использование / Utilisation
Call to_words on an integer, a float or an array in the locale of your choice.
require 'numbers_and_words'
I18n.with_locale(:en) { 42.to_words }
# => "forty-two"
I18n.with_locale(:es) { 42.to_words }
# => "cuarenta y dos"
I18n.with_locale(:ru) { 42.to_words }
# => "сорок два"
I18n.with_locale(:fr) { 42.to_words }
# => "quarante-deux"
I18n.with_locale(:de) { 42.to_words }
# => "zweiundvierzig"
I18n.with_locale(:en) { [1, 2, 3].to_words }
# => ["one", "two", "three"]
I18n.with_locale(:en) { 21.77.to_words }
# => "twenty-one and seventy-seven hundredths"Floats in scientific notation, such as 0.00001 (1.0e-05), are read from their exact decimal value.
I18n.with_locale(:en) { 0.00001.to_words }
# => "zero and one hundred-thousandth"Negative numbers start with the word for minus. They have no ordinal: ordinal: true raises NumbersAndWords::UnsupportedNumberError, an ArgumentError. Numbers past a language's largest scale word raise RangeError. Each language page gives its limit.
I18n.with_locale(:en) { -42.to_words }
# => "minus forty-two"
I18n.with_locale(:fr) { -1.5.to_words }
# => "moins un et cinq dixièmes"Command Line
The to_words command spells each argument, or each number on standard input, one per line.
$ to_words 42 1.5
forty-two
one and five tenths
$ to_words --locale ru 2000
две тысячи
$ echo 21 | to_words --ordinal
twenty-first
$ to_words --locale es --gender female 21
veintiunaChoose the language with -l/--locale (default en). Every option has a flag, such as --ordinal (and --no-ordinal), --gender VALUE or --precision N. The to_words --help command lists the flags and the languages that support each. Errors go to standard error, with exit status 1.
Supported Languages / Языки / Langues supportées
Each language page has examples, says whether it reads decimals, and documents its options. Features compares the languages.
| Language | Name | Code |
|---|---|---|
| Afrikaans* | Afrikaans | af |
| Albanian* | Shqip | sq |
| Arabic* | العربية | ar |
| Armenian | Հայերեն | hy |
| Azerbaijani* | Azərbaycan dili | az |
| Belarusian* | Беларуская | be |
| Bengali* | বাংলা | bn |
| Brazilian Portuguese | Português brasileiro | pt-BR |
| British English | English (British) | en-GB |
| Bosnian* | Bosanski | bs |
| Bulgarian* | Български | bg |
| Catalan* | Català | ca |
| Burmese* | မြန်မာဘာသာ | my |
| Chinese* | 中文 | zh |
| Croatian* | Hrvatski | hr |
| Czech | Čeština | cs |
| Danish | Dansk | da |
| Dutch | Nederlands | nl |
| English | English | en |
| Esperanto* | Esperanto | eo |
| Estonian | Eesti | et |
| Filipino* | Filipino | fil |
| Finnish* | Suomi | fi |
| French | Français | fr |
| Georgian | ქართული | ka |
| German | Deutsch | de |
| Greek* | Ελληνικά | el |
| Hebrew* | עברית | he |
| Hindi* | हिन्दी | hi |
| Hungarian | Magyar | hu |
| Icelandic* | Íslenska | is |
| Indonesian | Bahasa Indonesia | id |
| Irish* | Gaeilge | ga |
| Italian | Italiano | it |
| Japanese* | 日本語 | ja |
| Kazakh | Қазақша | kk |
| Khmer* | ភាសាខ្មែរ | km |
| Korean* | 한국어 | ko |
| Lao* | ພາສາລາວ | lo |
| Latvian | Latviešu | lv |
| Lithuanian | Lietuvių | lt |
| Macedonian* | Македонски | mk |
| Malay* | Bahasa Melayu | ms |
| Maltese* | Malti | mt |
| Norwegian Bokmål* | Norsk bokmål | nb |
| Persian* | فارسی | fa |
| Polish* | Polski | pl |
| Portuguese | Português | pt |
| Romanian* | Română | ro |
| Russian | Русский | ru |
| Serbian* | Српски | sr |
| Slovak* | Slovenčina | sk |
| Slovenian* | Slovenščina | sl |
| Spanish | Español | es |
| Swahili* | Kiswahili | sw |
| Swedish* | Svenska | sv |
| Swiss French | Français (Suisse) | fr-CH |
| Tamil* | தமிழ் | ta |
| Thai* | ภาษาไทย | th |
| Turkish | Türkçe | tr |
| Ukrainian | Українська | uk |
| Urdu* | اردو | ur |
| Vietnamese | Tiếng Việt | vi |
| Welsh* | Cymraeg | cy |
* An asterisk marks an experimental language, which has known gaps or no native-speaker review yet, as its page explains. To help, see #255.
Regional locales use their language: en-AU, en-CA, en-IN and en-US use en; fr-CA uses fr; de-AT and de-CH use de; it-CH uses it; nb-NO, no and no-NO use nb; zh-CN and zh-SG use zh.
Options / Опции / Options
Pass options to to_words. The options below work in every language. Language options, such as gender, ordinal or remove_zero, are on each language page.
Precision
The precision option pads the fractional part of a float with zeros.
I18n.with_locale(:ru) { 0.1.to_words(precision: 3) }
# => "ноль целых и сто тысячных"
I18n.with_locale(:es) { 0.2.to_words(precision: 2) }
# => "cero con veinte centésimas"Micro Separator
The micro_separator option replaces the word between the integral and fractional parts of a float.
I18n.with_locale(:en) { 21.77.to_words(micro_separator: 'plus') }
# => "twenty-one plus seventy-seven hundredths"Fallback
A locale with no bundled rules, or one the configuration disables, raises NumbersAndWords::UnsupportedLanguageError. Set fallback to a bundled locale to convert in that locale instead, or to true to return the digits.
I18n.with_locale(:am) { 42.to_words(fallback: :fr) }
# => "quarante-deux"
I18n.with_locale(:am) { 42.to_words(fallback: true) }
# => "42"Other options are validated for the fallback locale. A locale missing from I18n.available_locales never reaches the gem: I18n.with_locale raises I18n::InvalidLocale first. To set a fallback for every call, see Configuration.
Unit
The unit option follows a number with a unit word, such as a currency, in the form the number calls for. The gem ships no units: unit: :ruble looks up units.ruble in your application's translations, and "money.ruble" looks up units.money.ruble. A unit has one form per plural category of the language, such as one, few, many and other in Russian and Ukrainian, or one and other in English. A unit that never changes is one string. An optional gender sets the number's gender in languages with a gender option. A gender passed to to_words overrides it.
en:
units:
dollar: { one: dollar, other: dollars }
cent: { one: cent, other: cents }
fr:
units:
euro: { one: euro, other: euros }
centime: { one: centime, other: centimes }
ru:
units:
ruble: { one: рубль, few: рубля, many: рублей, other: рубля, gender: male }
kopeck: { one: копейка, few: копейки, many: копеек, other: копейки, gender: female }
zh:
units:
yuan: 元
he:
units:
shekel: { one: שקל, other: שקלים, gender: male }
agora: { one: אגורה, other: אגורות, gender: female }
ar:
units:
dinar: { zero: دينار, one: دينار, two: ديناران, few: دنانير, many: دينارًا, other: دينار }
fils: { one: فلس, two: فلسان, few: فلوس, many: فلسًا, other: فلس }With these translations:
I18n.with_locale(:en) { 1.to_words(unit: :dollar) }
# => "one dollar"
I18n.with_locale(:en) { 21.to_words(unit: :dollar) }
# => "twenty-one dollars"
I18n.with_locale(:ru) { 21.to_words(unit: :ruble) }
# => "двадцать один рубль"
I18n.with_locale(:ru) { 22.to_words(unit: :kopeck) }
# => "двадцать две копейки"
I18n.with_locale(:ru) { 11.to_words(unit: :kopeck) }
# => "одиннадцать копеек"
I18n.with_locale(:ru) { [1, 5].to_words(unit: :ruble) }
# => ["один рубль", "пять рублей"]
I18n.with_locale(:zh) { 21.to_words(unit: :yuan) }
# => "二十一元"The unit follows the number after a space, or with no space in Chinese, Japanese and Thai. Hebrew and Arabic put one after its unit, and Arabic says two with the dual form alone, as their pages describe. Before a unit, a number takes the form it has before a noun, listed under numbers.before_unit in the locale file. Examples are German "ein Euro" and "eine Mark", Swedish "en krona", Italian "un euro", Romanian "un leu" and "o liră", Hungarian "két forint" and Spanish "un euro" (see apocopated on its page). The unit's gender picks between these forms. With ordinal: true the unit is singular, since an ordinal names one item: the twenty-first dollar. Decimals keep their cardinal form.
I18n.with_locale(:en) { 21.to_words(unit: :dollar, ordinal: true) }
# => "twenty-first dollar"
I18n.with_locale(:ru) { 22.to_words(unit: :kopeck, ordinal: true) }
# => "двадцать вторая копейка"A unit without a translation, or missing both the form a number needs and the other form, raises NumbersAndWords::UnsupportedOptionError, such as The unit :euro has no translation in :ru, expected at units.euro.. Unit forms are nominative, so unit with another case raises too.
In Spanish, French, Italian and Portuguese, a whole number that ends in a scale word of a million or more takes de (Italian di) before the unit. One that ends in another word, such as mil, doesn't. French shortens de to d' before a vowel. A unit sets elision: true for a mute h (d'hectares), or elision: false for another exception. Romanian puts de in the unit forms, as it does for any number from 20.
I18n.with_locale(:fr) { 2_000_000.to_words(unit: :euro) }
# => "deux millions d'euros"After a decimal, the unit takes its other form, which is the genitive singular in Russian and Czech. French and Portuguese use the singular below 2, and Romanian uses the few form. A float with no fractional part reads as a whole number.
I18n.with_locale(:en) { 1.5.to_words(unit: :dollar) }
# => "one point five dollars"
I18n.with_locale(:ru) { 1.5.to_words(unit: :ruble) }
# => "одна целая и пять десятых рубля"
I18n.with_locale(:fr) { 1.5.to_words(unit: :euro) }
# => "un virgule cinq euro"The subunit option spells an amount in two units, such as dollars and cents. The decimal places, rounded half to even, become the subunit. The precision option sets how many places make one subunit, 2 by default. A zero subunit is left out unless zero_subunit: true. A zero unit is left out with remove_zero: true, where the language supports it. The two parts are joined with the language's word (and in English, et in French), attached to the subunit in Hebrew and Arabic (ו, و), or else with a space.
I18n.with_locale(:en) { 21.5.to_words(unit: :dollar, subunit: :cent) }
# => "twenty-one dollars and fifty cents"
I18n.with_locale(:ru) { 22.22.to_words(unit: :ruble, subunit: :kopeck) }
# => "двадцать два рубля двадцать две копейки"
I18n.with_locale(:fr) { 21.01.to_words(unit: :euro, subunit: :centime) }
# => "vingt et un euros et un centime"
I18n.with_locale(:en) { 21.to_words(unit: :dollar, subunit: :cent, zero_subunit: true) }
# => "twenty-one dollars and zero cents"
I18n.with_locale(:en) { 0.5.to_words(unit: :dollar, subunit: :cent, remove_zero: true) }
# => "fifty cents"Passing subunit without unit, subunit with ordinal: true, or zero_subunit without subunit raises NumbersAndWords::UnsupportedOptionError.
Unsupported Options
Every language accepts precision, micro_separator, fallback, unit, subunit and zero_subunit, plus the options on its page. Any other option or value is ignored with a warning that lists the supported ones. Version 2.0 will raise NumbersAndWords::UnsupportedOptionError, an ArgumentError, instead. Set strict_options to true to raise now.
Vietnamese has no grammatical gender, so 21.to_words(gender: :female) in vi returns "hai mươi mốt" and warns once:
numbers_and_words: The option :gender is not supported in :vi; supported: :micro_separator, :ordinal, :precision, :remove_zero, :subunit, :unit, :zero_subunit. It is ignored; set NumbersAndWords.config.strict_options = true to raise, the default in 2.0.
Deprecations
Three defaults change in 2.0: fraction: true in English, French, Italian and Portuguese, spelling: :traditional in French, and hundreds_with_union: false in British English. Pass the option to choose. The locale codes se, ua and kz are deprecated; use sv, uk and kk. Each deprecation warns once per process. Warnings about defaults are Ruby deprecation warnings, shown only with Warning[:deprecated] = true or ruby -W:deprecated. See UPGRADING.
Configuration / Конфигурация
All bundled locales load by default. To load only some, configure the gem after requiring it:
NumbersAndWords.configure do |config|
config.locales = %i[en ru]
endOr set it directly:
NumbersAndWords.config.locales = %i[en ru]Regional locales such as "en-US" become their bundled locale. Setting nil restores all bundled locales and [] loads none. An unsupported locale raises ArgumentError and changes nothing. An assignment takes effect at once. A configure block takes effect when it completes, and not at all if it raises.
The fallback setting chooses the default fallback; the fallback option of to_words overrides it.
NumbersAndWords.config.fallback = :enThe strict_options setting is false by default: an unsupported option or value is ignored with a warning. Set it to true to raise NumbersAndWords::UnsupportedOptionError, as 2.0 will by default.
NumbersAndWords.config.strict_options = trueConfiguration only changes this gem's locale files in I18n.load_path and its plural rules. It keeps application translations and an assigned I18n.available_locales. Converting in a disabled locale raises NumbersAndWords::UnsupportedLanguageError (Language not configured) unless a fallback is set.
Require the gem before looking up translations, as Rails does, and configure it during initialization. Configuring reloads the I18n backend, as assigning I18n.load_path does, so store runtime-only translations afterwards. Configuring again doesn't duplicate load paths. The gem never changes I18n.locale or I18n.default_locale, so don't disable their locales.
Requirements / Требования / Configuration requise
- The gem requires Ruby 3.1 or later, including JRuby.
- The gem requires I18n and is tested with 1.6 and later.
Users / Пользователи / Utilisateurs
See Users for applications and gems that use numbers_and_words.
Upgrading / Обновление / Mise à jour
See UPGRADING for changes in behavior and what 2.0 will change.
Contributing / Участие / Contribuer
See CONTRIBUTING, AGENTS, CHANGELOG, UPGRADING, BENCHMARKS and RELEASING.
License / Лицензия / Licence
The gem is released under the MIT License, see LICENSE.