Skip to content

Maheshkumar-Kakade/number-to-text

Repository files navigation

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 (German), tr (Turkish), id (Indonesian)

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

const 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', 'tr'
    • separator - Default: , for (en-us and en-in)
    • case - Default: titleCase - possible Values : "titleCase" , "lowerCase" , "upperCase" for (en-us and en-in)

Examples

const 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

const numberToText = require('numberToText')

class EnCustomConverter extends numberToText.Converter { // use language code as converter prefix
    constructor () {
        super()
        numberToText.addConverter(""/*enter language name here*/, this);
    }

    convertToText (num, options) {
        /* Implement number to text conversion logic */
    }
}

module.exports = new EnCustomConverter()

Tests

npm test

License

MIT