javascript - angularjs 中的 Math.ceil 返回错误值

标签 javascript angularjs math

当我通过控制台运行 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:

(...)

  1. If m is +0 or −0, return the String "0".

关于javascript - angularjs 中的 Math.ceil 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44522491/

相关文章:

找到最有效的基数来存储大整数的算法

c++ - 数值稳定的角平分线算法

jquery - jQuery 中包含指数吗?

javascript - 跟踪源自 JavaScript 的函数调用的代码行/文件

angularjs - ng-change在文本输入上不起作用

javascript - Flash 消息在 angularJS 中不起作用

javascript - 如何测试销毁范围

基于 AngularJS 的 UI 组件,quantumui vs. angular-strap vs. angular-bootstrap-ui

javascript - 在选项卡更改时将焦点设置到 Bootstrap TabContent 中的输入

javascript - 无法使用 jQuery 设置宽度