smart-round

Round big numbers with arbitrary precision

Usage no npm install needed!

<script type="module">
  import smartRound from 'https://cdn.skypack.dev/smart-round';
</script>

README

smart-round

Build Status Code Style Known Vulnerabilities

Round big numbers with arbitrary precision

Installation

$ npm install --save smart-round

Usage

The module exports a function that let you initialize a rounder function.

smartRound(maxPrecision, minDecimals, maxDecimals)

maxPrecision: Amount of significant digits to return. minDecimals: Minimum amount of decimals to return. maxDecimals: Maximum amount of decimals to return.

rounder(number, shouldFormat)

number: The value to round as any type supported by bignumber.js package. shouldFormat: Whether the returned string should be formatted or not, using accounting package.

Example

const smartRound = require('smart-round')

const rounder1 = smartRound(6, 0, 6)

console.log(rounder1('1234.56789', false)) // '1234.57'
console.log(rounder1('1234.56789', true)) // '1,234.56'

const rounder2 = smartRound(4, 2, 6)

console.log(rounder2('1234', true)) // '1,234.00'
console.log(rounder2('0.000123456', true)) // '0.000123'

ES5 support

A transpiled version is available in es5/index.js. To use that specific version, this syntax is also supported:

var smartRound = require('smart-round/es5');

License

MIT