Skip to content

dy/pretty-number

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#pretty-number unstable

Format number so it gets more humane format.

Usage

npm install pretty-number

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:

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 — handle SI prefixes, supposedly works well with pretty-number.
  • mumath — set of practical math utils.

About

Format number to more humane form than toFixed

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published