1.0.2 • Published 3 years ago

round-format-nums v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Round and format numbers

NPM NPM NPM NPM

Installation

npm i round-format-nums

Importing

const { roundDp, formatNumUnit } = require('round-format-nums')

roundDp(number, decimalPlaces)

// Examples
roundDp(234.07, 1) // 234.1
roundDp(23.9, 0) // 24
roundDp(23.9) // 24

Default for decimalPlaces is 0

formatNumUnit(number, decimalPlaces, config)

It places a unit at the end of number such as 1000 becomes '1K', you can change these configurations in the config argument which is an array of objects.

// default config
[
  { symbol: 'K', minDigits: 4, maxDigits: 6 },
  { symbol: 'M', minDigits: 7, maxDigits: 9 },
  { symbol: 'B', minDigits: 10, maxDigits: Infinity },
]

Explanation of { symbol: 'K', minDigits: 4, maxDigits: 6 }:

Numbers from 1000 (4 digits) to 999999 (6 digits) will be divided by 1000 and rounded off to your desired decimal places, and 'K' will be placed at the end.

Explanation of { symbol: 'M', minDigits: 7, maxDigits: 9 }:

Numbers from 1000000 (7 digits) to 999999999 (9 digits) will be divided by 1000000 (A million) and rounded off to your desired decimal places, and 'M' will be placed at the end.

maxDigits: Infinity means any amount of digits above the minDigits

// Example
formatNumUnit(12345, 2) // 12.35K according to the default config