Closed or4 closed 3 years ago
Also keep in mind big.js
is super slow in calculation, which is not ideal.
See the benchmark.
Just have to deal with that problematic, and I agree with @LesterLyu there is no need for big.js
for such use case. You would need it only if you need number with more than 15 significant digits.
@or4 you should use toPrecision(15)
instead of toFixed(15)
so you can cut the number precision whatever the number of digit before or after the dot. See https://stackoverflow.com/a/36369703
Both JS and Excel use IEEE-754 double precision which can have 15 to 17 significant digits. While Excel retain 15 digits when representing number, JS can retain up to 17 digits with the risk of having inaccurate 16th & 17th digits. While it's better for computation, it's not desirable for representation where you should limit to 15 digits to have only accurate digits
Demo in JS, you can compare that toPrecision(15)
works as in Excel (when formatting with enough decimal)
n = 0.2**-2;
console.log(n);
console.log(Number(n.toFixed(15)));
console.log(Number(n.toPrecision(15)));
console.log();
n = 0.1+0.2;
console.log(n);
console.log(Number(n.toFixed(15)));
console.log(Number(n.toPrecision(15)));
console.log();
n = 1234567890123451234;
console.log(n);
console.log(Number(n.toFixed(15)));
console.log(Number(n.toPrecision(15)));
console.log();
24.999999999999996
24.999999999999996
25
0.30000000000000004
0.3
0.3
1234567890123451100
1234567890123451100
1234567890123450000
@LesterLyu should the documentation updated with that information that can help people to deal with that ? And the demo too ?
hi, gus, thanks a lot for answers it is very useful, thanks again!
Please help, how I should work with cases like 0.1+0.2, I got invalid value, I fixed it by toFixed(15), but I have problem with case 0.2^-2, where result is 24.999999999996 Should I create fork and implement math ops with big.js?