javascript toFixed() rounding bug?

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

javascript toFixed() rounding bug?

Post by ersa »

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
Contact:

Re: javascript toFixed() rounding bug?

Post by schapel »

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.
User avatar
j.j.
Posts: 53
Joined: October 18th, 2007, 6:19 pm
Location: Franconia

Re: javascript toFixed() rounding bug?

Post by j.j. »

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.
User avatar
j.j.
Posts: 53
Joined: October 18th, 2007, 6:19 pm
Location: Franconia

Re: javascript toFixed() rounding bug?

Post by j.j. »

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

Re: javascript toFixed() rounding bug?

Post by ersa »

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
Contact:

Re: javascript toFixed() rounding bug?

Post by schapel »

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.
Post Reply