npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

comma-number

v2.1.0

Published

Format a number with commas or custom character

Downloads

732,694

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