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

swarm-numberformat

v0.4.0

Published

Format very large numbers in several human-readable ways. `format(1e6)` -> `"1 million"` or `"1M"`

Downloads

137

Readme

swarm-numberformat

Format large numbers in several human-readable ways. Designed for incremental games like Swarm Simulator.

See it in action, and a list of all suffixes.

Travis build status Dependency Status devDependency Status

Features

Several built-in formats to choose from. Let your users pick their favorite in an options menu!

 numberformat.format(1e10)    // or {format: 'standard'}
 // => "10.000 billion"
 numberformat.format(1e10, {format: 'scientific'})
 // => "1.0000e10"
 numberformat.format(1e10, {format: 'engineering'})
 // => "10.000E9"
 numberformat.format(1e10, {format: 'longScale'})
 // => "10.000 milliard"

At 1e249, 'standard' and 'longScale' fall back to scientific notation.

Use formatShort() or format({flavor:'short'}) to easily abbreviate suffixes and sigfigs.

 numberformat.formatShort(1e10)
 // => "10.0B"
 numberformat.formatShort(1e10, {format: 'longScale'})
 // => "10.0Md"

Of course, you can override significant figures.

 numberformat.formatShort(1e10, {sigfigs: 7})
 // => "10.00000B"

Use a formatter object instead of numberformat.format() / numberformat.numberformat to set your own default parameters.

 var f = new numberformat.Formatter({format: 'engineering', sigfigs: 2})
 f.format(1.2345e10)
 // => "12E9"

If you need numbers bigger than Number.MAX_VALUE (1e308), there's support for decimal.js.

 numberformat.format(new Decimal('1e10000'), {backend: 'decimal.js', format: 'engineering'})
 // => "10e9999"

decimal.js-light, break_infinity.js and other Decimal.js-compatible number objects are supported too. Pass their constructor to your formatter.

decimal.js-light example:

 var Decimal = require('decimal.js-light') // or <script src="decimal.js-light">; load it in whatever way works for your app
 numberformat.format(new Decimal('1e10000'), {backend: 'decimal.js', format: 'engineering', Decimal: Decimal})
 // => "10e9999"
 

break_infinity.js example:

 var Decimal = require('break_infinity.js') // or <script src="break_infinity.js">; load it in whatever way works for your app
 numberformat.format(new Decimal('1e10000'), {backend: 'decimal.js', format: 'engineering', Decimal: Decimal})
 // => "10e9999"

numberformat can parse its own output.

 numberformat.parse('10k')
 // => 10000
 numberformat.parse('10 thousand')
 // => 10000
 numberformat.parse('10,000')
 // => 10000
 numberformat.parse('10x')
 // => NaN
 numberformat.parse('', {'default': 3})
 // => 3

swarm-numberformat includes no third-party dependencies, and is less than 20k minified.

The suffixes used here are available in JSON format - this might be useful if your program isn't in Javascript, but can read JSON: standard-suffixes, long-scale-suffixes.

Getting started

<script src="//cdn.rawgit.com/erosson/swarm-numberformat/v0.4.0/dist/swarm-numberformat.min.js"></script>

or

bower install --save swarm-numberformat

or

npm install --save swarm-numberformat

const numberformat = require('swarm-numberformat')

Full API documentation. Also see the demo and a list of all suffixes.

Related work

The suffixes used by standard and longScale formats are based on http://home.kpn.nl/vanadovv/BignumbyN.html

This project started life as number formatting filters for Swarm Simulator.

https://www.npmjs.com/package/written-number has a lot in common with this project. It has better support for internationalization, but its suffixes stop at smaller numbers, and it has no decimal.js support.

Project template: https://github.com/babel/generator-babel-boilerplate

License

MIT - use this anywhere. I'd like it if you open-sourced any changes you make to this library (send a pull request? Github fork?), but it's not required.