8
I had a problem with Javascript today that I will describe:
I have a collection of values, for example:
US 11.3123
Brazil -0.2291
UK 0.4501
I want to show the values without the decimal places, round, and the result is this:
US 11
Brazil -0
UK 0
The problem is that "Brazil" has the value "-0" instead of "0".
I can even fix it easily:
html += '<tr><td>' + arr[i].Country + '</td>' +
'<td>' + d3.format(arr[i].Value || 0)) + '</td></tr>';
But, why Javascript shows the value with negative sign?
Updating
I’m using D3, and it’s transmitting Javascript behavior instead of returning 0 without signal as pure JS does.
But my question still continues, because on the console I type Math.round(-0.02) and is returned -0.
Are you sure there’s nothing else in between? My tests always returned "0" and not "-0".
– Filipe Moraes
@Filipe What browser are you using? In Chrome, when printing
Math.round(-0.2)
on the console got-0
.– mgibsonbr
I used the Chrome
– Filipe Moraes