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

pretty-num

v0.5.2

Published

Lightweight module for formatting numbers to a human readable string

Downloads

5,665

Readme

pretty-num

NPM Package Minified Size Build Status Coverage Status License: MIT

Lightweight module to convert number to a pretty human readable string.

Includes:

  • from-exponential - remove exponential notation
  • thousands - add thousands separators
  • toPrecision - adjust precision of decimal part
  • stripZeros - strip unnecessary leading and trailing zeros

Install

npm install pretty-num

Usage

import prettyNum, {PRECISION_SETTING} from 'pretty-num';

prettyNum(12.123e-10); // => '0.0000000012123'
prettyNum(0.00123456, {precision: 3}); // => '0.001'
prettyNum(0.00123456, {precision: 3, precisionSetting: PRECISION_SETTING.REDUCE_SIGNIFICANT}); // => '0.00123'
prettyNum(12345678.12345, {thousandsSeparator: ' '}); // => '12 345 678.12345'
prettyNum(12345678.12345, {decimalSeparator: ','}); // => '12345678,12345'
prettyNum('00123456789.12300e-2', {precision: 3, thousandsSeparator: ' '}); // => '1 234 567.891'

Options

thousandsSeparator

Defines the thousand grouping separator character

prettyNum(12345678.12345, {thousandsSeparator: ' '}); 
// => '12 345 678.12345'
prettyNum(12345678.12345, {thousandsSeparator: ','}); 
// => '12,345,678.12345'

separateOneDigit

Should number less than 10000 (e.g. 9999) to be separated, true by default

prettyNum(1234, {thousandsSeparator: ' ', separateOneDigit: true});
// => '1 234'
prettyNum(1234, {thousandsSeparator: ' ', separateOneDigit: false});
// => '1234'

precision

Number of decimal digits to keep when rounding. Pass falsey value to not change precision.

decimalSeparator

Separator between the integer part and the fractional part

precisionSetting

How to work with precision:

Reduce

1, REDUCE (default) - reduce precision to specified number of decimal digits, strip unnecessary ending zeros;

prettyNum(0.01023456, {precision: 3});
// => '0.01'
prettyNum(0.00001203456, {precision: 3});
// => '0'

Reduce significant

2, REDUCE_SIGNIFICANT - reduce precision to specified number of significant decimal digits, strip unnecessary ending zeros. Useful when rounding small values and they should not be rounded to 0

prettyNum(0.01023456, {precision: 3, precisionSetting: PRECISION_SETTING.REDUCE_SIGNIFICANT});
// => '0.0102'
prettyNum(0.00001203456, {precision: 3, precisionSetting: PRECISION_SETTING.REDUCE_SIGNIFICANT});
// => '0.000012'

Fixed

3, FIXED - set precision to specified number of decimal digits, pad with ending zeros if needed.

prettyNum(0.01023456, {precision: 3, precisionSetting: PRECISION_SETTING.FIXED});
// => '0.010'
prettyNum(0.00001203456, {precision: 3, precisionSetting: PRECISION_SETTING.FIXED});
// => '0.000'

Increase

4, INCREASE - pad with ending zeros to increase precision to specified number of decimal digits.

prettyNum(0.01, {precision: 4, precisionSetting: PRECISION_SETTING.INCREASE});
// => '0.0100'
prettyNum(12, {precision: 4, precisionSetting: PRECISION_SETTING.INCREASE});
// => '12.0000'
prettyNum(12.123456, {precision: 4, precisionSetting: PRECISION_SETTING.INCREASE});
// => '12.123456'

roundingMode

Specifies a rounding behavior for numerical operations capable of discarding precision. Following rounding modes are supported:

  • 1, UP - Rounding mode to round away from zero.
  • 2, DOWN - Rounding mode to round towards zero.
  • 3, CEIL - Rounding mode to round towards positive infinity.
  • 4, FLOOR - Rounding mode to round towards negative infinity.
  • 5, HALF_UP - Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
  • 6, HALF_DOWN - Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
  • 7, HALF_EVEN - Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

Extensive description of the modes can be found at Rounding Modes

prettyNum(123.657, {precision: 1, roundingMode: ROUNDING_MODE.DOWN}); // => "123.6"
prettyNum(123.657, {precision: 2, roundingMode: ROUNDING_MODE.CEIL}); // => "123.66"

Comparison

  • This module: Minified Size Minified Size

  • js-big-decimal: Minified Size Minified Size Math operations are supported, REDUCE_SIGNIFICANT, FIXED and INCREASE precisionSetting are not supported

  • big.js: Minified Size Minified Size Math operations are supported, some precisionSetting are not supported, CEIL, FLOOR and HALF_DOWN roundingMode are not supported.

  • bignumber.js: Minified Size Minified Size Math operations are supported, more rounding modes are supported, some precisionSetting are not supported.

License

MIT License