# pretty-num

> Lightweight module for formatting numbers to a human readable string

Latest version **0.6.2** (published 2025-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install pretty-num
pnpm add pretty-num
yarn add pretty-num
bun add pretty-num
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2025-03-27 |
| First published | 2018-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 43 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | shrpne |
| Maintainers | shrpne |
| Keywords | format, pretty, big, number, human, readable, string, text, exponential, thousands, separator, significant, round, decimal, fixed, decrease, precision |

## Links

- npm: https://www.npmjs.com/package/pretty-num
- Repository: https://github.com/shrpne/pretty-num
- Homepage: https://github.com/shrpne/pretty-num#readme
- Issues: https://github.com/shrpne/pretty-num/issues
- npm.io page: https://npm.io/package/pretty-num

## Dependencies (2)

- [thousands](https://npm.io/package/thousands.md) ^1.0.1
- [from-exponential](https://npm.io/package/from-exponential.md) ^1.1.1

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 0.6.2 (latest) — 2025-03-27
- 0.6.1 — 2025-03-27
- 0.6.0 — 2025-02-01
- 0.5.2 — 2022-06-08
- 0.5.1 — 2022-05-20
- 0.5.0 — 2020-11-30
- 0.4.0 — 2020-03-03
- 0.3.0 — 2019-06-17
- 0.2.2 — 2018-12-13
- 0.2.1 — 2018-12-11
- 0.2.0 — 2018-12-11
- 0.1.1 — 2018-08-28
- 0.1.0 — 2018-08-28
- 0.0.1 — 2018-07-31

## README

# pretty-num

[![NPM Package](https://img.shields.io/npm/v/pretty-num.svg?style=flat-square)](https://www.npmjs.org/package/pretty-num)
[![Minified Size](https://img.shields.io/bundlephobia/min/pretty-num.svg?style=flat-square)](https://bundlephobia.com/result?p=pretty-num)
[![Build Status](https://img.shields.io/travis/com/shrpne/pretty-num/master.svg?style=flat-square)](https://travis-ci.com/shrpne/pretty-num)
[![Coverage Status](https://img.shields.io/coveralls/github/shrpne/pretty-num/master.svg?style=flat-square)](https://coveralls.io/github/shrpne/pretty-num?branch=master)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://github.com/shrpne/pretty-num/blob/master/LICENSE)

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

Includes:
- [from-exponential](https://github.com/shrpne/from-exponential) - remove exponential notation
- [thousands](https://github.com/scurker/thousands) - add thousands separators
- toPrecision - adjust precision of decimal part
- stripZeros - strip unnecessary leading and trailing zeros


## Install

```
npm install pretty-num
```


## Usage

```js
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
```js
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
```js
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; 
```js
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
```js
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.
```js
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.
```js
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](https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html)

```js
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](https://img.shields.io/bundlephobia/min/pretty-num.svg?style=flat-square&label=minified)](https://bundlephobia.com/result?p=pretty-num) [![Minified Size](https://img.shields.io/bundlephobia/minzip/pretty-num.svg?style=flat-square&label=gzipped)](https://bundlephobia.com/result?p=pretty-num)
- [`js-big-decimal`](https://github.com/royNiladri/js-big-decimal): [![Minified Size](https://img.shields.io/bundlephobia/min/js-big-decimal.svg?style=flat-square&label=minified)](https://bundlephobia.com/result?p=js-big-decimal) [![Minified Size](https://img.shields.io/bundlephobia/minzip/js-big-decimal.svg?style=flat-square&label=gzipped)](https://bundlephobia.com/result?p=js-big-decimal) Math operations are supported, `REDUCE_SIGNIFICANT`, `FIXED` and `INCREASE` `precisionSetting` are not supported
- [`big.js`](https://github.com/MikeMcl/big.js): [![Minified Size](https://img.shields.io/bundlephobia/min/big.js.svg?style=flat-square&label=minified)](https://bundlephobia.com/result?p=big.js) [![Minified Size](https://img.shields.io/bundlephobia/minzip/big.js.svg?style=flat-square&label=gzipped)](https://bundlephobia.com/result?p=big.js) Math operations are supported, some `precisionSetting` are not supported, `CEIL`, `FLOOR` and `HALF_DOWN` `roundingMode` are not supported.

- [`bignumber.js`](https://github.com/MikeMcl/bignumber.js): [![Minified Size](https://img.shields.io/bundlephobia/min/bignumber.js.svg?style=flat-square&label=minified)](https://bundlephobia.com/result?p=bignumber.js) [![Minified Size](https://img.shields.io/bundlephobia/minzip/bignumber.js.svg?style=flat-square&label=gzipped)](https://bundlephobia.com/result?p=bignumber.js) Math operations are supported, more rounding modes are supported, some `precisionSetting` are not supported.



## License

MIT License

---
_Source: https://npm.io/package/pretty-num · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
