Sebi B Sebi B -4 years ago 119
AngularJS Question

Math.ceil in angularjs returns wrong value

When I run

Math.ceil(-0.001)
via console it returns
-0
, which
also corresponds to the offical documentation.

But if I'm running
Math.ceil(-0.001)
in my angularjs project, it
returns
0
, which is actually not correct.

I wanted to seperate the digits between the comma, that's why I faced this problem.

Answer Source

I'm not too familiar with the internals of Angular's template code, but I believe it uses toString() on the variables that are to be displayed inside the template.

toString() is defined to return the string "0" if used on the Number type with value -0. You can try this out for yourself:

console.log(Math.ceil(-0.001).toString());

As @JamesThorpe mentioned in his comment, this behaviour is explicitly defined in the ECMA2015 standard. Quoting the document:

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".
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download