round-decimal
v0.1.0
Published
Decimal rounding with ULP.
Downloads
6
Maintainers
Readme
round-decimal
Decimal rounding with ULP.
Warning
Work in process
Installation
NPM
npm install -D round-decimal
CDN
jsDelivr
Load roundDecimal
from jsDelivr.
<script src="https://cdn.jsdelivr.net/npm/round-decimal"></script>
unpkg
Load roundDecimal
from unpkg.
<script src="https://unpkg.com/round-decimal"></script>
Usage
Syntax
roundDecimal(value, minSignificantFractionDigits = 3)
Description
Decimal rounding with ULP.
Parameters
value
- A number.
minSignificantFractionDigits
- Minimum significant fraction digits.
- Default:
3
Return value
The rounded value.
Examples
import roundDecimal from 'round-decimal';
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(roundDecimal(0.1 + 0.2)); // 0.3
console.log(-0.1 - 0.2); // -0.30000000000000004
console.log(roundDecimal(-0.1 - 0.2)); // -0.3
console.log(10000000000.1 + 10000000000.2); // 20000000000.300003
console.log(roundDecimal(10000000000.1 + 10000000000.2)); // 20000000000.3
console.log(roundDecimal(10000000000.1 + 10000000000.2, 6)); // 20000000000.300003
console.log(10000000000000.1 + 10000000000000.2); // 20000000000000.297
console.log(roundDecimal(10000000000000.1 + 10000000000000.2)); // 20000000000000.297
console.log(roundDecimal(10000000000000.1 + 10000000000000.2, 2)); // 20000000000000.3
console.log(roundDecimal(1.3549999999999998)); // 1.355
Try it out on JSFiddle.