Home

Awesome

js-written-number

Gitter Build Status Code Climate Coverage Status Dependency Status devDependency Status Analytics npm downloads per month npm version


Convert numbers to words - their written form.

Install

With npm:

npm install --save written-number

With bower:

bower install written-number

Usage

var writtenNumber = require('written-number');
writtenNumber(1234); // => 'one thousand two hundred and thirty-four'

writtenNumber.defaults.lang = 'es';
writtenNumber(4758); // => 'cuatro mil setecientos cincuenta y ocho'

writtenNumber(1234, {lang: 'fr'});   // => 'mille deux cent trente-quatre'
writtenNumber(1234, {lang: 'es'});   // => 'mil doscientos treinta y cuatro'
writtenNumber(1234, {lang: 'az'});   // => 'min iki yüz otuz dörd'
writtenNumber(1234, {lang: 'pt'});   // => 'mil duzentos e trinta e quatro'
writtenNumber(1234, {lang: 'ar'});   // => 'ألف ومائتان وأربعة وثلاثون'
writtenNumber(1234, {lang: 'eo'});   // => 'mil ducent tridek kvar'
writtenNumber(1234, {lang: 'vi'});   // => 'một ngàn hai trăm và ba mươi bốn'
writtenNumber(1234, {lang: 'uk'});   // => 'одна тисяча двісті тридцять чотири'
writtenNumber(1234, {lang: 'id'});   // => 'seribu dua ratus tiga puluh empat'

Options

Currently supported languages are:

Languagelang
Englishen
Portuguese (Brazil)pt
Portuguese (Portugal)ptPT
Spanishes
Frenchfr
Esperantoeo
Vietnamesevi
Arabicar
Azerbaijanaz
Turkishtr
English (Indian)enIndian
Ukrainianuk
Indonesianid
Russianru

Contributing

Configure your own language

Each language has its own unique grammar exceptions. You can create your own language.json file in the folder "i18n" and give writtenNumber support for it. I don't think the current scheme and logic cover all the cases, but may be cover some.

The following parameters have been used for the currently available languages:

Language parameters

ParameterTypeDescriptionExamples
useLongScalebooleanIndicates if it uses long or short scale.This differs the meaning of the words billion, trillion and so on.
baseSeparatorstringSeparates the base cardinal numbers.29 -> twenty-eight. Spanish uses the connector " y "
unitSeparatorstringSeparates the units from the last base cardinal numbers.1234 -> one thousand two hundred and thirty-four
allSeparatorstringSeparates all cardinals, not only the last one.1125 -> ألف ومائة وخمسة وعشرون
baseObjectBase cardinals numbers. Numbers that have unique names and are used to build others.
alternativeBaseObjectAlternative versions of base cardinals numbers for usage with specific units. These bases will be treated as an extension for the default base."alternativeBase": { "feminine": {"1":"одна","2":"дві"} }
unitsArrayA list of number units (string or Object). Gives support to singular, dual an plural units. Check the Object parameters below.
unitExceptionsObjectSometimes grammar exceptions affect the base cardinal joined to the unit. You can set specific exceptions to any base cardinal number.Converting 1232000 in Spanish: Without Exception (Wrong): -> uno millón doscientos treinta y dos mil With Exception: -> un millón doscientos treinta y dos mil

Units parameters

A unit can be:

Unit parameterDescriptione.g. of languages
singularOne element.All
dualTwo elements.ar
pluralTwo or more elements. (or 3 or more)All
fewBetween 2 and 4 including.uk
useAlternativeBaseOverwrites default base.uk
useBaseInsteadUse the base cardinal number instead.es,hu,pt
useBaseExceptionSpecify with which unit (1 to 9) you don't want to use the base, and instead use the regular behavior.es,hu,pt
avoidPrefixExceptionUnits not using the base cardinal number prefix for unit 1.id,tr,it
avoidInNumberPluralUnits not using the plural form with trailing numbers other than 0.fr
restrictedPluralPlural only for 3 to 10. Singular if >= 11.ar
useSingularEndingUse singular form for numbers ending with 1.uk
useFewEndingUse few form for numbers ending with 2, 3 or 4.uk
avoidEndingRulesPlural form used instead of useSingularEnding and useFewEndinguk

Versioning

Do your changes and submit a PR. If you've write access and want to bump the version, run mversion [major|minor|patch] -m. That'll bump both bower.json and package.json.

License

This code is licensed under the MIT license for Pedro Tacla Yamada. For more information, please refer to the LICENSE file.