/number-to-text

number-to-text

Primary LanguageJavaScript

number-to-text

number-to-text is a regular expression driven number to text converter (no mathematical operation used). supported languages en-us (US English), en-in (Indian English), de (Germany)

Build Status npm version Test Coverage js-standard-style

NPM

Index

Install

npm package

npm install number-to-text --save

bower package

bower install number-to-text --save

Usage

node

var numberToText = require('number-to-text')
require('number-to-text/converters/en-us');

numberToText.convertToText(12346)

numberToText.convertToText("12346")

bower

<script src="bower_components/dist/number-to-text.js"></script>
<!-- creates numberToText object on window -->
<script>
    console.log(numberToText.convertToText(123456));
</script>

convertToText(num, options)

Arguments

  • num - a string or number.
  • options
    • language - Default: en-us - possible Values : en-us, 'en-in' , 'de'
    • separator - Default: , for (en-us and en-in)
    • case - Default: titleCase - possible Values : "titleCase" , "lowerCase" , "upperCase" for (en-us and en-in)

Examples

var numberToText = require('number-to-text')
require('number-to-text/converters/en-us'); // load converter

numberToText.convertToText('123456')  
//One Hundred Twenty Three Thousand, Four Hundred Fifty Six

numberToText.convertToText(123456) 
//One Hundred Twenty Three Thousand, Four Hundred Fifty Six

numberToText.convertToText(123456,{case:"lowerCase"}) 
//one hundred twenty three thousand, four hundred fifty six

numberToText.convertToText(123456,{case:"upperCase"}) 
//ONE HUNDRED TWENTY THREE THOUSAND, FOUR HUNDRED FIFTY SIX

numberToText.convertToText('123456',{separator : '', case:"upperCase"}) 
//ONE HUNDRED TWENTY THREE THOUSAND FOUR HUNDRED FIFTY SIX

addConverter(language, langConverter)

function is used to implement your own language converter.

Arguments

  • language - a language code.
  • langConverter - an object inehrited from numberToText.Converter

writing own language converter

var numberToText = require('numberToText')
var util = require('util')

function EnCustomConverter () { // use language code as converter prefix
    numberToText.addConverter(""/*enter language name here*/, this);
}

util.inherits(EnCustomConverter, numberToText.Converter)


EnCustomConverter.prototype.convertToText = function (num, options) {
    /* Implement number to text conversion logic */
}

module.exports = new EnCustomConverter()

Tests

npm test

License

MIT