https://github.com/dy/pretty-number
Format number to more humane form than toFixed
https://github.com/dy/pretty-number
Last synced: 11 months ago
JSON representation
Format number to more humane form than toFixed
- Host: GitHub
- URL: https://github.com/dy/pretty-number
- Owner: dy
- Created: 2016-10-31T22:11:54.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-01T04:51:07.000Z (over 9 years ago)
- Last Synced: 2025-02-12T18:59:39.886Z (over 1 year ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
#pretty-number [](http://github.com/badges/stability-badges)
Format number so it gets more humane format.
## Usage
[](https://npmjs.org/package/pretty-number/)
```js
const pretty = require('pretty-number');
pretty(10e+20) //10²¹
pretty(12e+20) //1.2 × 10²¹
pretty(1) //1
pretty(0) //0
pretty(-1) //−1
pretty(1.555e+20) //1.555 × 10²⁰
pretty(1.555e-15) //1.555 × 10⁻¹⁵
pretty(Infinity) //∞
pretty(-Infinity) //−∞
pretty(7.2973525664e-3, 3) //7.297 352 5664 × 10⁻³
pretty(72973525.664) //72 973 525.664
pretty(.123) //0.123
pretty(-.123) //−0.123
pretty(1.5) //1.5
pretty(-1.5) //−1.5
pretty(1e100) //10¹⁰⁰
pretty(-1e100) //−10¹⁰⁰
pretty(1e-100) //10⁻¹⁰⁰
pretty(-1e-100) //−10⁻¹⁰⁰
//indicate wishful precision
pretty(-0.000000005) //0
pretty(-0.000000005, 12) //−0.000 000 005
```
Formatting can be modified via constants:
```js
pretty.multSign = '×';
pretty.minusSign = '−';
pretty.spaceSign = ' ';
pretty.fractSign = '.';
pretty.precision = 9;
```
Note also that to make result look good custom font may be needed.
## Related
* [human-format](https://github.com/JsCommunity/human-format) — handle SI prefixes, supposedly works well with pretty-number.
* [mumath](https://github.com/dfcreative/mumath) — set of practical math utils.