MozillaZine

javascript toFixed() rounding bug?

Discussion of bugs in Mozilla Firefox
ersa
 
Posts: 2
Joined: December 18th, 2008, 3:29 am

Post Posted December 18th, 2008, 3:34 am

im not sure if this is new but here is the problem i encountered with javascript:

var num = new Number(0.595);
alert(num.toFixed(2)); // 0.59

where is IE 7, it correctly alerts 0.60.

even funnier:

var num = new Number(2.595);
alert(num.toFixed(2)); // 2.60

schapel
 
Posts: 3483
Joined: November 4th, 2002, 10:47 pm
Location: Ann Arbor, Michigan

Post Posted December 18th, 2008, 3:25 pm

It's due to the way floating point numbers are represented as binary numbers by computers. The numbers 0.595 and 2.595 cannot be represented exactly in binary, so they are represented as a number either slightly higher or slightly lower than that number. If it is higher, the number will round up. If it is lower, it will round down. The bottom line is that it is not a bug. You are expecting floating point numbers to be precise when they are not.

j.j.

User avatar
 
Posts: 53
Joined: October 18th, 2007, 6:19 pm
Location: Franconia

Post Posted December 18th, 2008, 10:38 pm

ersa wrote:where is IE 7, it correctly alerts 0.60.

IE has real bugs with toFixed(). Try alert((0.9).toFixed(0));

j.j.

j.j.

User avatar
 
Posts: 53
Joined: October 18th, 2007, 6:19 pm
Location: Franconia

Post Posted December 18th, 2008, 11:03 pm

schapel wrote:The bottom line is that it is not a bug
... in the Browser.

But it's a ](*,) in the ECMAscript standard. They plan a workaround in ES4:

http://www.builderau.com.au/program/javascript/soa/Why-I-Love-ECMAScript-4-Real-Decimals/0,339028434,339289678,00.htm

http://wiki.ecmascript.org/doku.php?id=proposals:decimal&s=use+decimal

j.j.

ersa
 
Posts: 2
Joined: December 18th, 2008, 3:29 am

Post Posted December 19th, 2008, 1:47 am

schapel wrote:It's due to the way floating point numbers are represented as binary numbers by computers. The numbers 0.595 and 2.595 cannot be represented exactly in binary, so they are represented as a number either slightly higher or slightly lower than that number. If it is higher, the number will round up. If it is lower, it will round down. The bottom line is that it is not a bug. You are expecting floating point numbers to be precise when they are not.

thanks. it explains.
to be holy honest, i was not "expecting floating point numbers to be precise", i was simply expecting it to do as explained in "https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed". and well, unexpectedly received a 'surprise'.

schapel
 
Posts: 3483
Joined: November 4th, 2002, 10:47 pm
Location: Ann Arbor, Michigan

Post Posted December 19th, 2008, 5:31 am

Yes, you were expecting floating point numbers to be precise. That's why you got the surprise, because the numbers you were using could not be represented precisely exactly in binary floating point.

Return to Firefox Bugs


Who is online

Users browsing this forum: No registered users and 2 guests