-1

In my case, i am converting a string value of '9999999999999999' to integer using parseFloat(). But it converts to next number of it i.e. 10000000000000000. But i need to convert it to before of that number i.e. 999999999999999998. I have searched for a while in google. But could not get clear idea to implement this.

3 Answers3

0

Try this

document.getElementById("demo").innerHTML=Math.round(9999999999999999-2);

OUTPUT

9999999999999998

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
0

This number is too big to represented precisely in JavaScript Number value. So no amount of conversion will give you values reliably/precisly as you want around such range.

I.e. (9999999999999999-1)===(9999999999999999) returns true, but (9999999999999998)===(9999999999999999) returns false.

If you need such high precision in JavaScript (similar to many other languages) you need to use specialized data types (unfortunately there is no "BigInteger" type built in in JavaScript).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

You will need to use some external javascript library to work with big numbers like that, cause max number you cant represent without losing presicion in javascript integers is 9007199254740992 (Explanation : What is JavaScript's highest integer value that a Number can go to without losing precision?)

Here you have some link where people discuss about some libraries to use for javascript big numbers. How to deal with big numbers in javascript

Community
  • 1
  • 1
Xavi
  • 196
  • 2
  • 9