嘿,伙计们,我只是在想JS Math.round(),我们都知道,由于IEEE754,数字有一些问题。但我想知道"/ 100“和+"e-2”在这里是否有区别。
我们知道:
Math.round(1.005 * 1000 ) / 1000 /* result 1.005 */
Math.round(1.005 * 100 ) / 100 /* result 1 - which is not 1.01 as we expected but this is not a surprise*/但这两种情况是否等价,或者在其他情况下是否存在一些隐藏的差异:
Math.round(1.005e2) / 100 /* result in 1.01 */
/* vs */
+(Math.round(1.005e2)+"e-2") /* result in 1.01 */在本例中,它们产生相同的结果,但它们总是这样做吗?
只是好奇而已!谢谢
发布于 2020-11-02 01:12:37
它不仅是滥用强制规则的坏主意,还违反了Infinity / -Infinity
> Math.round(Infinity)/100
Infinity
> +(Math.round(Infinity)+"e-2")
NaN也就是-0
> +(Math.round(-0)+"e-2")
0
> Math.round(-0)/100
-0如果我在代码评审中看到+"e-2",我可能会尖叫~和我的经理开始对话:)
https://stackoverflow.com/questions/64634566
复制相似问题