0

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/

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
  • 1
    I suggest you get acquainted with MDN docs about `MAX_SAFE_INTEGER` [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER). I think that is connected with some restrictions about length integers numbers. – Arkadiusz Wieczorek May 18 '17 at 07:36
  • 1
    you might want to read [this](https://medium.com/the-node-js-collection/javascripts-number-type-8d59199db1b6) – Max Koretskyi May 18 '17 at 07:36
  • 1
    Do you get the value as a string? What is expected result? See also [How do I add 1 to a big integer represented as a string in JavaScript?](http://stackoverflow.com/questions/43614407/how-do-i-add-1-to-a-big-integer-represented-as-a-string-in-javascript/) – guest271314 May 18 '17 at 07:39
  • 1
    https://mikemcl.github.io/bignumber.js/ – Emissary May 18 '17 at 07:48
  • It will be great if you can create an example for me . I am not able to do it thanks – user7386493 May 18 '17 at 08:00

1 Answers1

4

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.

null_pointer
  • 1,779
  • 4
  • 19
  • 38
rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
  • How do I solve this in JavaScript , coz I have a requirement for to store it as same as 9 sixteen times – user7386493 May 18 '17 at 07:39
  • How about using the value a million times larger and adjust the display? Watch out for maximum value of floating number, though. – Raptor May 18 '17 at 09:29
  • The thing is I am making use of accounting js to formatMoney method on blur event and unformatMoney on keydown event at that time when i pass the value 9999999999999999 to unformatMethod , it becomes 10000000000000000. So I need to solve this – user7386493 May 18 '17 at 12:53