Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 9 of 9
Thread: toFixed failing in IE

12072006, 10:50 AM #1
 Join Date
 Jan 2004
 Location
 London, England
 Posts
 95
 Thanks
 0
 Thanked 0 Times in 0 Posts
toFixed failing in IE
I don't suppose this is news, but it is to me.
Code:<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>
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?
12072006, 02:20 PM
#2
 Join Date
 Jun 2005
 Location
 Near Chicago, IL, USA
 Posts
 1,973
 Thanks
 1
 Thanked 32 Times in 31 Posts
MSDN documentation states that toFixed does no rounding.
http://msdn2.microsoft.com/enus/library/sstyff0z.aspx
The toFixed method returns a string representation of a number in fixedpoint 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...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.
If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
12072006, 02:49 PM
#3
 Join Date
 Jan 2004
 Location
 London, England
 Posts
 95
 Thanks
 0
 Thanked 0 Times in 0 Posts
12072006, 03:09 PM
#4
 Join Date
 Dec 2004
 Location
 In a place far, far away...
 Posts
 19,291
 Thanks
 2
 Thanked 1,043 Times in 1,019 Posts
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.
12072006, 05:15 PM
#5
 Join Date
 Mar 2006
 Posts
 730
 Thanks
 36
 Thanked 132 Times in 123 Posts
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.
var N=1.379,n=2;Code: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 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
//
Last edited by mrhoo; 12072006 at 05:25 PM.
12072006, 05:25 PM
#6
 Join Date
 Feb 2003
 Location
 Umeå, Sweden
 Posts
 5,575
 Thanks
 0
 Thanked 83 Times in 74 Posts
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:Code: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.
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ  HTML & CSS Docs, FAQ  XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
12072006, 07:15 PM
#7
 Join Date
 Jan 2004
 Location
 London, England
 Posts
 95
 Thanks
 0
 Thanked 0 Times in 0 Posts
Ah, it is a known fault then, OK. Isn't that a disgrace?
Thanks for suggestions, folks.
Here's an extended example showing it works only sometimes...
Code:<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>
12072006, 07:32 PM
#8
 Join Date
 Mar 2006
 Posts
 730
 Thanks
 36
 Thanked 132 Times in 123 Posts
Quit crying about your toolsmake 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)));
12082006, 10:53 AM
#9
 Join Date
 Jan 2004
 Location
 London, England
 Posts
 95
 Thanks
 0
 Thanked 0 Times in 0 Posts
Not a helpful response.Perhaps we can be a little simpler.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)));
N=123.4599;
a = N.toString()
a = a.substr(0,a.indexOf('.')+3)