...

View Full Version : toFixed failing in IE



Roy Gardiner
12-07-2006, 11:50 AM
I don't suppose this is news, but it is to me.

<html>
<body>
<script language="JavaScript" type="text/JavaScript">
<!-- Hide code from older browsers
document.write(
' 0.8.toFixed(0) ' + 0.8.toFixed(0) + " <br>" +
' 1.8.toFixed(0) ' + 1.8.toFixed(0) + " <br>" +
' 0.85.toFixed(0) ' + 0.85.toFixed(0) + " <br>" +
' 0.9.toFixed(0) ' + 0.9.toFixed(0) + " <br>" +
' 0.91.toFixed(0) ' + 0.91.toFixed(0) + " <br>" +
' 0.92.toFixed(0) ' + 0.92.toFixed(0) + " <br>" +
' 0.93.toFixed(0) ' + 0.93.toFixed(0) + " <br>" +
' 0.94.toFixed(0) ' + 0.94.toFixed(0) + " <br>" +
' 0.95.toFixed(0) ' + 0.95.toFixed(0) + " <br>" +
' 0.99.toFixed(0) ' + 0.99.toFixed(0) + " <br>" +
' '
)
-->
</script>
</body>
</html> works as expected in FF and Opera, but not in IE6; the answers there are

0.8.toFixed(0) 0
1.8.toFixed(0) 2
0.85.toFixed(0) 0
0.9.toFixed(0) 0
0.91.toFixed(0) 0
0.92.toFixed(0) 0
0.93.toFixed(0) 0
0.94.toFixed(0) 0
0.95.toFixed(0) 1
0.99.toFixed(0) 1

Comments and suggestions welcome. How is it in IE7, anyone?

nikkiH
12-07-2006, 03:20 PM
MSDN documentation states that toFixed does no rounding.
http://msdn2.microsoft.com/en-us/library/sstyff0z.aspx

The toFixed method returns a string representation of a number in fixed-point notation. The string contains one digit before the significand's decimal point, and must contain fractionDigits digits after it.


Mozilla DOES round.
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Number:toFixed
A string representation of number that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

Roy Gardiner
12-07-2006, 03:49 PM
MSDN documentation states that toFixed does no rounding.
http://msdn2.microsoft.com/en-us/library/sstyff0z.aspx

The toFixed method returns a string representation of a number in fixed-point notation. The string contains one digit before the significand's decimal point, and must contain fractionDigits digits after it.


Mozilla DOES round.
Isn't there supposed to be a general standard for this? In any case, the IE action is in error because it's neither truncating nor rounding consistently.

This must be a known problem surely to goodness?

_Aerospace_Eng_
12-07-2006, 04:09 PM
Maybe but it behaves the same in IE7 but if you are simply wanting the numbers to be rounded the just use the Math.round function.

mrhoo
12-07-2006, 06:15 PM
If you want to return the integer part of a number
in any browser use Math.floor(number) instead of .toFixed(0);

To get the same value with .toFixed(n) that IE returns (no rounding)
in other clients (which round the last significant digit in a float)
you must truncate the number.

To get a rounded value in IE as well as the others, you must round it
at the significant digit.


Number.prototype.toFixedTruncate= function(n){
var N= this;
N= Math.floor(N*Math.pow(10,n));
N/=Math.pow(10,n);
return(N.toFixed(n));
}
Number.prototype.toFixedRound= function(n){
var N= this;
N= Math.round(N*Math.pow(10,n));
N/=Math.pow(10,n);
return(N.toFixed(n));
}
var N=1.379,n=2;
var str=N+' Truncated: '+N.toFixedTruncate(n)+'\n'+N+' Rounded: '+N.toFixedRound(n);
alert(str)

The string returned is the same in all browsers:
1.379 Truncated: 1.37
1.379 Rounded: 1.38

//

liorean
12-07-2006, 06:25 PM
The problem is that JScript has a broken implementation of toFixed. In the FAQ I presented an alternative in the form of toDecimals. A problem with it is that it isn't written for the special case of 0 decimals, and as such doesn't remove the decimal point.

However, let me show you the result in comparison:
0.8: toFixed(0)=0 toDecimals(0)=1.
1.8: toFixed(0)=2 toDecimals(0)=2.
0.85: toFixed(0)=0 toDecimals(0)=1.
0.9: toFixed(0)=0 toDecimals(0)=1.
0.91: toFixed(0)=0 toDecimals(0)=1.
0.92: toFixed(0)=0 toDecimals(0)=1.
0.93: toFixed(0)=0 toDecimals(0)=1.
0.94: toFixed(0)=0 toDecimals(0)=1.
0.95: toFixed(0)=1 toDecimals(0)=1.
0.99: toFixed(0)=1 toDecimals(0)=1.

Roy Gardiner
12-07-2006, 08:15 PM
The problem is that JScript has a broken implementation of toFixed. In the FAQ I presented an alternative in the form of toDecimals.Ah, it is a known fault then, OK.
Maybe but it behaves the same in IE7 Isn't that a disgrace?

Thanks for suggestions, folks.




Here's an extended example showing it works only sometimes...:)


<html>
<body>
<script language="JavaScript" type="text/JavaScript">
<!-- Hide code from older browsers
document.write(
' 0.08.toFixed(1) ' + 0.08.toFixed(1) + " <br>" +
' 0.085.toFixed(1) ' + 0.085.toFixed(1) + " <br>" +
' 0.09.toFixed(1) ' + 0.09.toFixed(1) + " <br>" +
' 0.091.toFixed(1) ' + 0.091.toFixed(1) + " <br>" +
' 0.092.toFixed(1) ' + 0.092.toFixed(1) + " <br>" +
' 0.093.toFixed(1) ' + 0.093.toFixed(1) + " <br>" +
' 0.094.toFixed(1) ' + 0.094.toFixed(1) + " <br>" +
' 0.095.toFixed(1) ' + 0.095.toFixed(1) + " <br>" +
' 0.099.toFixed(1) ' + 0.099.toFixed(1) + " <br>" +
' 0.08.toFixed(1) ' + 0.08.toFixed(1) + " <br>" +
' <br> ' +
' 1.08.toFixed(1) ' + 1.08.toFixed(1) + " <br>" +
' 1.085.toFixed(1) ' + 1.085.toFixed(1) + " <br>" +
' 1.09.toFixed(1) ' + 1.09.toFixed(1) + " <br>" +
' 1.091.toFixed(1) ' + 1.091.toFixed(1) + " <br>" +
' 1.092.toFixed(1) ' + 1.092.toFixed(1) + " <br>" +
' 1.093.toFixed(1) ' + 1.093.toFixed(1) + " <br>" +
' 1.094.toFixed(1) ' + 1.094.toFixed(1) + " <br>" +
' 1.095.toFixed(1) ' + 1.095.toFixed(1) + " <br>" +
' 1.099.toFixed(1) ' + 1.099.toFixed(1) + " <br>" +
' 1.08.toFixed(1) ' + 1.08.toFixed(1) + " <br>" +
' <br> ' +
' 1.8.toFixed(0) ' + 1.8.toFixed(0) + " <br>" +
' 0.85.toFixed(0) ' + 0.85.toFixed(0) + " <br>" +
' 0.9.toFixed(0) ' + 0.9.toFixed(0) + " <br>" +
' 0.91.toFixed(0) ' + 0.91.toFixed(0) + " <br>" +
' 0.92.toFixed(0) ' + 0.92.toFixed(0) + " <br>" +
' 0.93.toFixed(0) ' + 0.93.toFixed(0) + " <br>" +
' 0.94.toFixed(0) ' + 0.94.toFixed(0) + " <br>" +
' 0.95.toFixed(0) ' + 0.95.toFixed(0) + " <br>" +
' 0.99.toFixed(0) ' + 0.99.toFixed(0) + " <br>" +
' '
)
-->
</script>
</body>
</html>

mrhoo
12-07-2006, 08:32 PM
Quit crying about your tools-make them work.

simplest workaround to guarantee IE type (truncated) return is to convert the float to a string and use substring to chop off the decimal digits-

N=123.4599;
n=2
var N=(N*Math.pow(10,n)/Math.pow(10,n))+'';
alert(N.substring(0,N.indexOf('.')+(n+1)));

Roy Gardiner
12-08-2006, 11:53 AM
Quit crying about your tools-make them work.Not a helpful response.

simplest workaround to guarantee IE type (truncated) return is to convert the float to a string and use substring to chop off the decimal digits-

N=123.4599;
n=2
var N=(N*Math.pow(10,n)/Math.pow(10,n))+'';
alert(N.substring(0,N.indexOf('.')+(n+1))); Perhaps we can be a little simpler.

N=123.4599;
a = N.toString()
a = a.substr(0,a.indexOf('.')+3)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum