5

GitHub - nick-lai/round-decimal: Decimal rounding with ULP.

 1 year ago
source link: https://github.com/nick-lai/round-decimal
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

round-decimal

Decimal rounding with ULP.

Warning
Work in process

Installation

npm install -D round-decimal

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.

License

MIT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK