0
About toFixed() in JS
I met with a challenge question like this: let sum = 0.1 + 0.2; console.log(+sum.toFixed(2)); The answer is 0.3. Why? I thought it should be 0.30. - Confused Boy
2 ответов
+ 3
Typecasted back into Number due to the plus operator before sum.
https://code.sololearn.com/W8ZgKlTH97cJ/?ref=app
換一個說法:
如果只有 sum.toFixed(2),結果是 0.30,因為是String。
但現在有 +號在前面,這個 +號因為前方無String,而後方可以parseFloat(),所以被判定為 Number.prototype.addition() ,而不是String.prototype.concantentation()。因此 0.30被轉化回Number,而小數點二位的0不是 significant figure,所以在過程中被捨去了。
0
Gordon 谢谢🙂