Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

comma-number

elidoran223.5kMIT2.1.0TypeScript support: definitely-typed

Format a number with commas or custom character

number, format, comma

readme

Comma number

Build Status npm version Coverage Status

Install

$ npm install --save comma-number

Usage

const commaNumber = require('comma-number')

commaNumber(1000) // "1,000"
commaNumber(-1000) // "-1,000"
commaNumber(-1000, '.') // "-1.000"

commaNumber(1000.12) // "1,000.12"
commaNumber(-1000.12) // "-1,000.12"
commaNumber('-1000,12', '.', ',') // "-1.000,12"

// make a new function using custom separator and decimal char:
const format = commaNumber.bindWith('_', '!')
// use it as you would commaNumber().
format(1000)     // "1_000"
format(-1000)    // "-1_000"
format(1000.12)  // "1_000!12"
format(-1000.12) // "-1_000!12"

Version 2 Changes

Revised implementation changes the API a bit:

  1. input with a type other than string and number is returned as is, not as '0'.
  2. supports decimals in the number
  3. a string number may use an alternate decimal character, specify it as the third argument
  4. added a bindWith function to use a currying style to bind options for a reusable format function.

Other changes:

  1. Added benchmarking to test implementation performance
  2. added code coverage
  3. added new badges in this README
  4. 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.

// the default commaNumber uses a comma separator and period for decimal char.
var commaNumber = require('comma-number')
  // can build a custom version using bindWith.
  , format = commaNumber.bindWith('_', '!')
  , result1 = commaNumber(1234567.89)
  , result2 = format('1234567.89')

console.log(result1) // outputs:  1,234,567.89
console.log(result2) // outputs:  1_234_567!89

Scripts for Testing, Benchmarking, and Code Coverage

# run tests via tap
$ npm test

# benchmark current implementation versus previous
npm run benchmark

# get coverage info by default with testing:
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.

MIT License

changelog

2.1.0 - 2021/04/30

  1. tweak the flow a bit so the separator and decimal value are all in an array which is turned into a string via join(''). so, all at once the parts are turned into the final string.
  2. added some local scopes to the format.
  3. changed how it calculates where to start putting separators.
  4. switched a while-loop with an increment in the block to a for-loop with the increment in its loop definition.
  5. fixed a bug in the benchmark/index.js file when using the --delta option.
  6. added more (longer) numbers to the tests

2.0.2 - 2021/04/28

  1. update dev dependencies
  2. add 2021 to copyright
  3. reformatted var usage in test/lib/index.js and added new test showing a bug
  4. fixed bug when a number input was given for a decimalChar other than '.'.
  5. added node 14 to testing
  6. switched from tape to tap for testing and removed istanbul
  7. added .nyc_output to git ignore
  8. updated travis config to use a single VM and use nave to run tests with multiple node versions.
  9. add npm install to command list in docs/benchmark.md.

2.0.1 - 2019/04/19

  1. update dev dependencies
  2. add 2018 and 2019 to copyright
  3. remove lcov only coverage script
  4. add nave dev dependency and use it in test scripts for node 4-12 (evens)
  5. use const/let instead of var
  6. use more descriptive variable names
  7. reformat a bit
  8. update benchmark script to report PID and ask before starting so the process can be set to a high priority

2.0.0 - 2017/04/02

  1. revised implementation
  2. supports decimals
  3. more test cases
  4. updated travis with modern node versions
  5. added badges to README
  6. updated README content with change info, new bindWith(), benchmark info.
  7. added code coverage
  8. dropped 'standard' lint
  9. added benchmarking
  10. added a document showing the benchmark output (docs/benchmark.md)

1.1.0 / 2015-07-16

  • Use isFinite instead of Number.isFinite for improved browser compatibility

1.0.0 / 2015-05-12

  • Initial release