View the Project on GitHub yamadapc/js-written-number

js-written-number

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


Convert numbers to their written form.

Install

npm i --save written-number

Usage

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

Options

Internationalization

Currently available languages are english, spanish and portuguese.

Spanish Example

var writtenNumber = require('written-number');
writtenNumber(1234, { lang: 'es' }); 
// => 'mil doscientos treinta y cuatro'
var writtenNumber = require('written-number');
writtennumber.defaults.lang = 'es';
writtenNumber(4758); 
// => 'cuatro mil setecientos cincuenta y ocho'

Options

Property Value
noAnd false
lang 'en'

Configure your own language

Each language has it's 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.

useLongScale:

'Boolean' that indicates if it use long or short scale. This differs the meaning of the words billion, trillion and so on.

baseSeparator:

'String' that separates the base cardinal numbers. Example: 29 -> twenty-eight. Spanish uses the conector " y ".

unitSeparator:

'String' that separates the units from the last base cardinal numbers. Example: 1234 -> one thousand two hundred and thirty-four

base:

Base cardinals numbers. Numbers that have unique names and are used to build others.

units:

Number units. It can be:

{
  "singular": "millón",
  "plural": "millones"
}

With useBaseException you can also specify with which unit (1 to 9) you don't want use the base cardinal instead and use the regular behaviour.

{
  "singular": "ciento",
  "useBaseInstead": true,
  "useBaseException": [1]
}

In some languages like spanish, specific units like "mil" does not use the base cardinal number prefix for unit 1.

{
  "singular": "mil",
  "avoidPrefixException": [1]
}
unitExceptions:

Sometimes grammar exceptions affect the base cardinal joined to the unit. You can set specific exceptions to any base cardinal number.

Spanish example:

Without Exception (Wrong): 1232000 -> **uno** millón doscientos treinta y dos mil
With Exception: 1232000 -> **un** millón doscientos treinta y dos mil

English configuration example

{
  "useLongScale": false,
  "baseSeparator": "-",
  "unitSeparator": "and ",
  "base": {
    "0": "zero",
    "1": "one",
    "2": "two",
    "3": "three",
    ...
    "90": "ninety"
  },
  "units" : [
    "hundred",
    "thousand",
    "million",
    "billion",
    "trillion",
    ...
    "quindecillion"
  ],
  "unitExceptions": []
}

Spanish configuration example

{
  "useLongScale": true,
  "baseSeparator": " y ",
  "unitSeparator": "",
  "base": {
    "0": "cero",
    "1": "uno",
    "2": "dos",
    "3": "tres",
    ...
    "1000": "mil"
  },
  "unitExceptions": {
    "1": "un"
  },
  "units" : [
    {
      "singular": "ciento",
      "useBaseInstead": true,
      "useBaseException": [1]
    },
    {
      "singular": "mil",
      "avoidPrefixException": [1]
    },
    {
      "singular": "millón",
      "plural": "millones"
    },
    ...
  ]
}

License

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