Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2006, 10:50 AM   PM User | #1
Roy Gardiner
New Coder

 
Join Date: Jan 2004
Location: London, England
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Gardiner is an unknown quantity at this point
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?
Roy Gardiner is offline   Reply With Quote
Old 12-07-2006, 02:20 PM   PM User | #2
nikkiH
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
nikkiH is on a distinguished road
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/
nikkiH is offline   Reply With Quote
Old 12-07-2006, 02:49 PM   PM User | #3
Roy Gardiner
New Coder

 
Join Date: Jan 2004
Location: London, England
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Gardiner is an unknown quantity at this point
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?
Roy Gardiner is offline   Reply With Quote
Old 12-07-2006, 03:09 PM   PM User | #4
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 12-07-2006, 05:15 PM   PM User | #5
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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..
mrhoo is offline   Reply With Quote
Old 12-07-2006, 05:25 PM   PM User | #6
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
liorean is offline   Reply With Quote
Old 12-07-2006, 07:15 PM   PM User | #7
Roy Gardiner
New Coder

 
Join Date: Jan 2004
Location: London, England
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Gardiner is an unknown quantity at this point
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>
Roy Gardiner is offline   Reply With Quote
Old 12-07-2006, 07:32 PM   PM User | #8
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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)));
mrhoo is offline   Reply With Quote
Old 12-08-2006, 10:53 AM   PM User | #9
Roy Gardiner
New Coder

 
Join Date: Jan 2004
Location: London, England
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Gardiner is an unknown quantity at this point
Quote:
Originally Posted by mrhoo View Post
Quit crying about your tools-make them work.
Not a helpful response.
Quote:
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)
Roy Gardiner is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:56 AM.


Advertisement
Log in to turn off these ads.