user7386493
user7386493

Reputation: 9

How to solve the issue of round number in JavaScript

I am having an issue in which if I am entering 9 sixteen times and formatting it using accounting js or even Math.round(9999999999999999) it is becoming 10000000000000000. How do I solve this issue .

Reference to accounting js or help me some the issue with math.random also.

http://openexchangerates.github.io/accounting.js/

Upvotes: 0

Views: 203

Answers (1)

rickvdbosch
rickvdbosch

Reputation: 15621

Floating point can't precisely represent all numbers. You'll ee this in integers as soon as your numbers are above 9,007,199,254,740,991 (Number.MAX_SAFE_INTEGER).

Beyond Number.MAX_SAFE_INTEGER + 1 (9007199254740992), the IEEE-754 floating-point format can no longer represent every consecutive integer because you no longer have a 1s bit; the lowest-order bit now represents multiples of 2. Eventually, if we keep going, we lose that bit and only work in multiples of 4. And so on.

Your values are well above that threshold, and so they get rounded to the nearest representable value.

Upvotes: 4

Related Questions