Craig
Craig

Reputation: 8294

Javascript's toFixed gives -0

When Javascript's toFixed() method rounds to 0 from the negative side it gives -0 as the response. AFAIK -0 is not a real number and the response should be just 0.

Number(0.4).toFixed(0) => 0
Number(-0.4).toFixed(0) => -0

I can force the right answer by subtracting 0 from the response, which I need to do since drawing -0 on my chart looks really bad, but is -0 correct under any circumstances?

Here's a link to a jsFiddle showing the problem and my solution: http://jsfiddle.net/4sk870Lm/

Upvotes: 1

Views: 270

Answers (1)

Craig
Craig

Reputation: 8294

To solve this problem I simply subtracted 0 from the answer toFixed() have and that will convert -0 to 0

Number(-0.4).toFixed(0) -0

Upvotes: 2

Related Questions