As we know, in Javascript 0.1 + 0.2 does not return 0.3 - it returns 0.3000000004.
I have a problem while I am building a calculator in Javascript.
Usually, we use the toFixed()
function to format a number using fixed-point notation.
But on a calculator, we can not expect the number of digits to appear after the decimal point.
So how can I solve this problem?
Asked
Active
Viewed 25 times
0

ZaO Lover
- 433
- 2
- 13
-
mistake - Javascript – ZaO Lover Jan 21 '21 at 01:12
-
@HovercraftFullOfEels - This exact same issue exists in Java too - :-) – Randy Casburn Jan 21 '21 at 01:13
-
Do you have any idea about how to solve that issue? – ZaO Lover Jan 21 '21 at 01:14
-
If you're open to using a library, this will do the trick https://www.npmjs.com/package/two-sum – Flaom Jan 21 '21 at 01:14
-
1Actually, on a calculator you *can* expect the number of significant digits: it's how many fit on your screen. If you have only 10 or so, floating point double precision will suffice. – Bergi Jan 21 '21 at 01:15
-
"*0.1 + 0.2 does not return 0.3 - it returns 0.3000000004.*" - actually it's 0.30000000000000004 – Bergi Jan 21 '21 at 01:18