Comma number
Install
$ npm install --save comma-number
Usage
const commaNumber = require('comma-number')
commaNumber(1000)
commaNumber(-1000)
commaNumber(-1000, '.')
commaNumber(1000.12)
commaNumber(-1000.12)
commaNumber('-1000,12', '.', ',')
const format = commaNumber.bindWith('_', '!')
format(1000)
format(-1000)
format(1000.12)
format(-1000.12)
Version 2 Changes
Revised implementation changes the API a bit:
- input with a type other than
string
and number
is returned as is, not as '0'
. - supports decimals in the number
- a string number may use an alternate decimal character, specify it as the third argument
- added a
bindWith
function to use a currying style to bind options for a reusable format function.
Other changes:
- Added benchmarking to test implementation performance
- added code coverage
- added new badges in this README
- added more versions to the Travis CI config
API
commaNumber(number, [separator=','], [decimalChar='.'])
Parameters:
- number : {(Number|String)} Number to format
- separator : {String} Value used to separate numbers
- decimalChar : {String} Value used to separate the decimal value
Returns:
- {String} Comma formatted number
bindWith(separator, decimalChar)
The commaNumber
function accepts these same parameters as the second and third params. This prevents using currying to bind them and reuse that bound function.
The bindWith
function accepts the options and returns a function bound with them.
var commaNumber = require('comma-number')
, format = commaNumber.bindWith('_', '!')
, result1 = commaNumber(1234567.89)
, result2 = format('1234567.89')
console.log(result1)
console.log(result2)
Scripts for Testing, Benchmarking, and Code Coverage
$ npm test
npm run benchmark
npm test
Performance Comparison
The rewrite has a considerable performance increase from the previous version.
I converted the benchmark output from my machine into a table.
It compares the performance of version 1.1.0 with 2.0.0. The inputs with decimals can only be processed by the new version so those show as "invalid" for the previous version.