num2words

Convert a number to a word representation.

Usage

var num2words = require( '@stdlib/string/num2words' );

num2words( value[, options] )

Converts a number to a word representation.

var out = num2words( 87 );
// returns 'eighty-seven'

out = num2words( 23101 );
// returns 'twenty-three thousand one hundred one'

out = num2words( 0.53 );
// returns 'zero point five three'

The function accepts the following options:

  • lang: string indicating the language. Default: 'en'.

By default, the function returns a word representation of a number in English. To return a word representation of a number in a different language, set the lang option.

var out = num2words( 22, {
    'lang': 'de'
});
// returns 'zweiundzwanzig'

out = num2words( 0.53, {
    'lang': 'de'
});
// returns 'null Komma fünf drei'

Notes

  • The following languages are supported:

    • en: English.
    • de: German.

Examples

var num2words = require( '@stdlib/string/num2words' );

var out = num2words( 29 );
// returns 'twenty-nine'

out = num2words( 113 );
// returns 'one hundred thirteen'

out = num2words( 13.52 );
// returns 'thirteen point five two'

out = num2words( 47, {
    'lang': 'de'
});
// returns 'siebenundvierzig'

CLI

Usage

Usage: num2words [options] [<string>]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.
         --split sep           Delimiter for stdin data. Default: '/\\r?\\n/'.
         --lang lang           Language. Default: 'en'.

Notes

  • If the split separator is a regular expression, ensure that the split option is either properly escaped or enclosed in quotes.

    # Not escaped...
    $ echo -n $'15\n23' | num2words --split /\r?\n/
    
    # Escaped...
    $ echo -n $'15\n23' | num2words --split /\\r?\\n/
    
  • The implementation ignores trailing delimiters.

Examples

$ num2words '10'
ten

To use as a standard stream,

$ echo -n '23' | num2words
twenty-three

By default, when used as a standard stream, the implementation assumes newline-delimited data. To specify an alternative delimiter, set the split option.

$ echo -n '10.3\t23.1' | num2words --split '\t'
ten point three
twenty-three point one
Did you find this page helpful?