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

number-suffix

v1.1.0

Published

Turns a number to a fixed string with a metric or abbreviation suffix

Downloads

1,284

Readme

NumberSuffix

What does it do?

number-suffix is a package for rounding numbers with a suffix for its zeros. Example:

  • 1,234 -> 1k
  • 1,234,567 -> 1M

Featues

  • Precision
  • Fixed measurement
  • Adding more formatting styles. It supports by default:
    • Metric (default)
    • Abbreviation
  • Support for the following units:
    1. thousand
    2. million
    3. billion
    4. trillion
    5. quadrillion
    6. quintillion

Why not using number-abbreviate, numbr or numeraljs?

Mainly because of the libraries usage. I needed something very generic to work with, which had to be fast and not creating any instances. The other libraries had either a limited API (which works well on the wide internet) or they're creating new objects for each format operation. number-abbreviate was the closest thing I found that meets my expectation - but lacked one feature I needed: a fixed measurement for numbers.

Also, this library uses mainly mathematic function for calculations compared to other libraries that uses strings instead, leading to slower performance.

Getting started

Basic installation:

npm i number-suffix --save

Basic importing, supports both CJS and MJS

const NumberSuffix = require('number-suffix')
import NumberSuffix from 'number-suffix'
import {format} from 'number-suffix'

Usage

Basic usage:

NumberSuffix.format(1234) // 1k
NumberSuffix.format(1234567) // 1M
NumberSuffix.format(1e9) // 1G
NumberSuffix.format(1e12) // 1T

Precision:

NumberSuffix.format(1234, {precision: 2}) // 1.23k
NumberSuffix.format(1234567, {precision: 2}) // 1.23M

Fixed measurement: Supports: 'thousand', 'million', 'billion', 'trillion', 'quadrillion' and 'quintillion'.

NumberSuffix.format(1234567, {measurement: 'thousand'}) // 1234k
NumberSuffix.format(1234, {precision: 3, measurement: 'million'}) // 0.001M

Abbreviation:

NumberSuffix.format(1e3, {style: 'abbreviation'}) // 1K
NumberSuffix.format(1e9, {style: 'abbreviation'}) // 1B

And adding and using a new style:

NumberSuffix.addStyle('yourStyleName', ['Thousand', 'Million', 'Billion', 'Trillion'])
NumberSuffix.format(1e6, {style: 'yourStyleName'}) // 1Million

If there are null values it will lean on the metric style as a fallback.

Instance

You can create an instance of NumberSuffix for more specific usage without using global settings.

const numberSuffix = new NumberSuffix({...})

You can use fixed options in addition to the ones you have:

const numberSuffix = new NumberSuffix({style: 'abbreviation'})
numberSuffix.format(1e3) // 1K
const numberSuffix = new NumberSuffix({precision: 2})
numberSuffix.format(1234) // 1.23k
const numberSuffix = new NumberSuffix({measurement: 'thousand'})
numberSuffix.format(1234567) // 1234k

And, of course, to override them for even more specific usage:

const numberSuffix = new NumberSuffix({measurement: 'thousand'})
numberSuffix.format(1234567, {measurement: 'million'}) // 1M

You can add your own style as well with

const numberSuffix = new NumberSuffix()
numberSuffix.addStyle('myStyle', ['T', 'M', 'B', 'T'])
numberSuffix.setDefaultStyle('myStyle')

To change the fixed options you can just:

const numberSuffix = new NumberSuffix()
numberSuffix.setOptions({...})