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

approximate-number

v2.1.1

Published

Converts numbers into a more human-friendly format, similar to `ls`'s `--human-readable` flag (`ls -lh`) or Stack Overflow's reputation numbers. For example, 123456 becomes '123k'.

Downloads

19,399

Readme

Approximate Number

Abbreviates numbers into a more human-friendly format, similar to ls's --human-readable flag (ls -lh) or Stack Overflow's reputation numbers. For example, 123456 becomes '123k'.

Works in Node.js and in browsers.

var approx = require('approximate-number');

console.log(approx(1234));
//> 1.2k

console.log(approx(12345));
//> 12k

console.log(approx(1234, {decimal: false}));
//> 1k

console.log(approx(1234, {decimal: ','}));
//> 1,2k

console.log('My Stack Overflow reputation is %s.', approx(3671, {
  min10k: true
}));
//> My Stack Overflow reputation is 3,671.

console.log('The US national debt is %s.', approx(19939034457936, {
  prefix: '$', 
  capital: true, 
  round: true
}));
// > The US national debt is $20T.

// Truncates/floors numbers by default
approx(9999);
// > '9.9k'

// Set `round: true` to round instead of floor.
approx(9999,{round: true});
// > '10k'

Getting Started

Node.js

Install node module with: npm install --save approximate-number

var approx = require('approximate-number');
approx(123456) // 123k

Bower

Install with: bower install approximate-number

<script src="/bower_components/approximate-number/lib/approximate-number.js"></script>
<script>
alert(approximateNumber(1234567890)); // 1.2b
</script>

Browser usage without a package manager

Grab the latest approximate-number.js and copy it onto your site.

Options

  • separator {String|Boolean} Default = ','. Thousands separator - set to a string (e.g. '.') to use that string or false to not use any separator.
  • decimal {String|Boolean} Default = '.'. Decimal - set to a string (e.g. ',') to use that or set to false to avoid outputting values with a decimal.
  • round {Boolean} Default = false. Round numbers off rather than flooring/truncating. When true, 105000 would become '11m', when false it becomes '10m'.
  • min10k {Boolean} Default = false. Do not abbreviate numbers below 10000. E.g. 9999 would become '9,999' rather than '9k'. (Stack Overflow-style).
  • prefix {String} Default = ''. Optional string to prepend to the value, e.g. '$'.
  • suffix {String} Default = ''. Optional string to append to the value, e.g. '%'.
  • capital {Boolean} Default = false. Set to true to use capital letters, e.g. 3.9M instead of 3.9m
  • precision {Number} Default = undefined. Number of significant digits. Must be greater than 0. Use of this option forces rounding.

V2.1 Changes

  • Added precision

V2 Changes

  • Added optional configuration object
  • Changed default from rounding to truncation. Override with options.round=true.
  • Started abbreviating numbers between 1000 and 9999 by default. Override with options.min10k=true.
  • Added trillions support.
  • Dropped cli support.

License

Copyright (c) 2014 Nathan Friedly
Licensed under the MIT license.