当我通过控制台运行 Math.ceil(-0.001)
时,它返回 -0
,这
也对应官方documentation .
但是如果我在我的 angularjs 项目中运行 Math.ceil(-0.001)
,它
返回0
,这实际上是不正确的。
我想分隔逗号之间的数字,这就是我遇到这个问题的原因。
最佳答案
我不太熟悉 Angular 模板代码的内部结构,但我相信它对要在模板内显示的变量使用了 toString()
。
toString()
被定义为返回字符串 "0"
(如果用于值为 -0< 的
。您可以自己尝试一下:Number
类型)/
console.log(Math.ceil(-0.001).toString());
正如 @JamesThorpe 在他的评论中提到的,这种行为是 explicitly defined in the ECMA2015 standard 。引用文档:
7.1.12.1 ToString Applied to the Number Type
The abstract operation ToString converts a Number m to String format as follows:
(...)
- If m is +0 or −0, return the String
"0"
.
关于javascript - angularjs 中的 Math.ceil 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44522491/