Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    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>
    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?

  • #2
    Senior Coder nikkiH's Avatar
    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/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...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/

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    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?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    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.

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

    //
    Last edited by mrhoo; 12-07-2006 at 05:25 PM.

  • #6
    Master Coder
    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

  • #7
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean View Post
    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.
    Quote Originally Posted by _Aerospace_Eng_ View Post
    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...

    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>

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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)));

  • #9
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mrhoo View Post
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •