It's not possible to natively deal with a 21-digit precision number in JavaScript.
JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision ("double") value. As such, parseFloat in JavaScript is the equivalent of a "parse double" in other languages.
However, a number/"double" only provides 16 significant digits (decimal) of precision and so reading in a number with 21-digits will lose the 5 least significant digits1.
For more precision (or accuracy) a "big number" library must be used;
- What is the standard solution in JavaScript for handling big numbers (BigNum)?
1 Information can be lost when encoding as an IEEE "double", which cannot encode all decimal values exactly, but that's another question..
Answer from user2864740 on Stack OverflowIt's not possible to natively deal with a 21-digit precision number in JavaScript.
JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision ("double") value. As such, parseFloat in JavaScript is the equivalent of a "parse double" in other languages.
However, a number/"double" only provides 16 significant digits (decimal) of precision and so reading in a number with 21-digits will lose the 5 least significant digits1.
For more precision (or accuracy) a "big number" library must be used;
- What is the standard solution in JavaScript for handling big numbers (BigNum)?
1 Information can be lost when encoding as an IEEE "double", which cannot encode all decimal values exactly, but that's another question..
This many years later, we have native support for big integers. So if the number is an integer number, parse it as a BigInt:
Copyconst s = "12345678901234567890123";
const big = BigInt(s);
console.log("Number: " + big);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If the number has a fractional part, use a Big Decimal implementation. For instance, you could check out the one I posted here.
I am working through freecodecamp's walk through of building a full stack react app using firebase and ran into a strange issue.
I was able to post my request body successfully, but since it was an array, I used JSON.parse before adding it to firebase.
When I checked my DB, it came in as a string.
I couldn't figure it out until I saw on SO a suggestion to parse it twice, and that worked.
Can someone help me understand why the first parse produced just a string instead of the array and it required a second parse call?
Use parseInt().
var num = 2.9
console.log(parseInt(num, 10)); // 2
You can also use |.
var num = 2.9
console.log(num | 0); // 2
I find the "parseInt" suggestions to be pretty curious, because "parseInt" operates on strings by design. That's why its name has the word "parse" in it.
A trick that avoids a function call entirely is
var truncated = ~~number;
The double application of the "~" unary operator will leave you with a truncated version of a double-precision value. However, the value is limited to 32 bit precision, as with all the other JavaScript operations that implicitly involve considering numbers to be integers (like array indexing and the bitwise operators).
edit — In an update quite a while later, another alternative to the ~~ trick is to bitwise-OR the value with zero:
var truncated = number|0;
If you can explain in terms of C++ too, it would help in me understanding it better.