We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 82bfac2 commit ddf8d8cCopy full SHA for ddf8d8c
1-js/05-data-types/02-number/article.md
@@ -249,14 +249,14 @@ PHP,Java,C,Perl,Ruby 给出的也是完全相同的结果,因为它们
249
250
```js run
251
let sum = 0.1 + 0.2;
252
-alert( sum.toFixed(2) ); // 0.30
+alert( sum.toFixed(2) ); // "0.30"
253
```
254
255
请注意,`toFixed` 总是返回一个字符串。它确保小数点后有 2 位数字。如果我们有一个电子购物网站,并需要显示 `¥ 0.30`,这实际上很方便。对于其他情况,我们可以使用一元加号将其强制转换为一个数字:
256
257
258
259
-alert( +sum.toFixed(2) ); // "0.30"
+alert( +sum.toFixed(2) ); // 0.3
260
261
262
我们可以将数字临时乘以 100(或更大的数字),将其转换为整数,进行数学运算,然后再除回。当我们使用整数进行数学运算时,误差会有所减少,但仍然可以在除法中得到:
0 commit comments