View Full Version : Why doesn't toFixed work in Safari?
network23
04-02-2004, 06:36 PM
At least for me, it works fine in Mozilla, but not Safari...
<html><head>
<script language="JavaScript">
<!--
function calOTF(level) {
var blu = 556
var monthrate = .48
var mult
if ( level == "Basic" ) {
document.worksheet.motlir.value = 0
} else {
switch (level) {
case "x2" :
mult = 2
break
case "x3" :
mult = 3
break
case "x4" :
mult = 4
break
}
// Find monthly rate
var optionalRate = blu * mult * monthrate + .0099
var optionalRateFix = optionalRate.toFixed(2)
document.worksheet.motlir.value = optionalRateFix
}
}
// -->
</script>
</head>
<body>
<form name="worksheet">
<input type="button" size="10" value="Basic" name="oll" onClick="calOTF(this.value)"> <input type="button" size="10" value="x2" name="oll" onClick="calOTF(this.value)"> <input type="button" size="10" value="x3" name="oll" onClick="calOTF(this.value)"> <input type="button" size="10" value="x4" name="oll" onClick="calOTF(this.value)">
$<input type="text" name="motlir" size="10">
</form>
</body>
</html>
I don't understand! And if toFixed doesn't really work, what's the best/easiest way to "cut off" any places after the two after the decimal (don't want to round!)
Thank you for your help!
Willy Duitt
04-02-2004, 07:29 PM
toFixed() will only work on browsers which support Javascript 1.5
The browser compatability chart I use does not list Safari so I can not help you with confirming the compatability.
However, you can try this:
optionalRateFix = Math.round(optionalRateFix*100).toString();
optionalRateFix = optionalRateFix.substring(0,optionalRateFix.length-2)+'.'+
optionalRateFix.substring(optionalRateFix.length-2,optionalRateFix.length);
.....Willy
liorean
04-02-2004, 07:59 PM
Or you could have a look at the mechanism I wrote at Q: How do I format a number so that it always has two decimals? in the JavaScript FAQ. It should be a pretty complete replacement for [object Number].toFixed. Also, I can confirm that Number.prototype.toFixed does not exist in Safari 1.2.
network23
04-02-2004, 08:32 PM
Thank you!!!
I didn't want the rounding, and the result had to be a number, so I modified what you proposed to this...
optionalRateFix = (optionalRate*10000).toString(); //remove ./convert to string
optionalRateFix = optionalRateFix.substring(0,optionalRateFix.length-4)+'.'+
optionalRateFix.substring(optionalRateFix.length-4,optionalRateFix.length-2);
optionalRateFix = parseFloat(optionalRateFix) // convert to number
and it works great! Thanks again!
network23
04-02-2004, 08:34 PM
liorean,
Your link didn't go anywhere.
I tried A mechanism like that that substituted the toFixed function (can't say if it was yours or someone else's) and it didn't work for me either, and the code was too advanced for me to work it out to see where the problem was.
Roy Sinclair
04-02-2004, 08:45 PM
http://www.codingforums.com/showthread.php?p=178077#post178077 -- Seems to have been a couple extra zeroes in Liorean's link. His code is good though so I think you'll find it meets your needs nicely.
Willy Duitt
04-02-2004, 09:03 PM
I believe you misunderstood how Math.round was being used in my example. It is only rounding the number after it multiples it by 100. Therefore it is rounding the last digit which will be within the decimal.
EG:
Math.round(25.567 X's 100) = 2557
length-2 == 25
Add (.) == 25.
Plus length-2,length == 25.57
But heh, if multiplying by 1000 and all that works and your happy with it. ;)
.....Willy
Edit: Actually, the example I posted would return a more acurate number than the method you are using. Your method using my example would return 25.56 and you would lose a cent. And that doesn't make much cents to me. :D
network23
04-02-2004, 11:11 PM
Willy,
I understand completely. I'm just using the calculations given to me. I think they have their own rounding worked in earlier in their math, and the final step is to truncate the xxx.xxxx number down to xxx.xx. ( I think they add .0099 earlier on to do the roundup and then later remove the 3rd and 4th decimal places.)
In your example above, they really would want 25.56
Willy Duitt
04-02-2004, 11:29 PM
Perhaps, but, toFixed(2) would return 25.57
So if you were looking for a more cross-browser alternative to using toFixed() I still believe my example would return the value you are looking for.
For example if you are determining the points on a loan. Over 30 years that tenth of a point could amount to quite a few $'s.
.....Willy
DHTML Kitchen
04-03-2004, 10:56 AM
Willy,
I understand completely.
Your example shows you don't, my friend.
network23
04-05-2004, 09:08 PM
.toFixed(2) rounds?
Willy Duitt
04-05-2004, 09:12 PM
<script type="text/javascript">
var num = 25.567;
alert(num.toFixed(2))
</script>
.....Willy ;)
Edit: I previously meant to say hundredth but it still makes alotta cents. :eek:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.