View Full Version : Freaky decimal problem
Stoffel
02-04-2003, 04:57 PM
Hiya,
I have a script that calculates a total price
The script works perfectly BUT (It was to good to be true :D )
every time it has to do this calculation ( 9.9 + 19.8 ) it doestn't give 29.7 but 29.700000000000, does anyone know why?
beetle
02-04-2003, 05:11 PM
I suspect that you calculate some of those values on the fly, which will mean you get large decimals. Anyway, when working with monetary values you should always round or convert to currency (http://www.codingforums.com/showthread.php?s=&threadid=13577) :D
MrDoubtFire
02-04-2003, 05:22 PM
Maybe you could post the script that deals with this?
And like beetle says, use Math.round or convert it to currency (this of course, depends on whether you're actually dealing with currency I suppose).
var result = Math.round(original*100)/100;
This would give you the result to two decimal places.
MrDoubtFire
beetle
02-04-2003, 05:25 PM
Originally posted by MrDoubtFire
(this of course, depends on whether you're actually dealing with currency I suppose).He said "price" :D
MrDoubtFire
02-04-2003, 05:27 PM
Aha! My bad, I'm still only on the third cup of coffee...
MRDF
Stoffel
02-04-2003, 08:21 PM
This is the code:
function Totaal() {
var totaal ;
var sub1 = document.form.sub1.value ;
var sub2 = document.form.sub2.value ;
var sub3 = document.form.sub3.value ;
var sub4 = document.form.sub4.value ;
var sub5 = document.form.sub5.value ;
if ( (sub1 == "") && (sub2 == "") && (sub3 == "") && (sub4 == "") && (sub5 == "") ) {
alert("blabla");
document.form.total.value = ""; }
else { (sub1 == "")? sub1 = 0 : sub1 = Math.round(parseFloat(sub1) * 10) / 10;
(sub2 == "")? sub2 = 0 : sub2 = Math.round(parseFloat(sub2) * 10) / 10;
(sub3 == "")? sub3 = 0 : sub3 = Math.round(parseFloat(sub3) * 10) / 10;
(sub4 == "")? sub4 = 0 : sub4 = Math.round(parseFloat(sub4) * 10) / 10;
(sub5 == "")? sub5 = 0 : sub5 = Math.round(parseFloat(sub5) * 10) / 10;
total = sub1 + sub2 + sub3 + sub4 + sub5;
document.form.total.value = total ; }
}
As u see, I already tried the math.round (with parseFloat and parseInt)
beetle
02-04-2003, 08:33 PM
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function Totaal( f )
{
var total = 0;
for ( var i = 1; i <= 5; i++ )
total += round( f.elements['sub' + i].value );
f.total.value = total;
function round( num )
{
var newNum = Math.round( parseFloat( num ) * 100 ) / 100;
return ( isNaN( newNum ) ) ? 0 : newNum;
}
}
</script>
<body>
<form>
<input type="text" name="sub1" /><br />
<input type="text" name="sub2" /><br />
<input type="text" name="sub3" /><br />
<input type="text" name="sub4" /><br />
<input type="text" name="sub5" /><br />
<br />
<input type="button" value="Get Total" onclick="Totaal( this.form )" /><br />
<input type="text" name="total" />
</form>
</body>
</html>
Stoffel
02-04-2003, 08:48 PM
Thnx but it still gives the wrong number.
Its only with these calculations
9.9 + 19.8
19.8 + 39.6
( in this example I worked with 2 movies wich prices were 9.9 for 1 movie, and the max amount they could order was 5 movies)
Stoffel
02-04-2003, 08:49 PM
What's the difference between > and />?
MrDoubtFire
02-04-2003, 08:57 PM
The "/>" indicates XHTML markup instead of plain HTML.
MrDoubtFire
beetle
02-04-2003, 09:14 PM
Oops, sorry. I missed one thing...function Totaal( f )
{
var total = 0;
for ( var i = 1; i <= 5; i++ )
total += round( f.elements['sub' + i].value );
f.total.value = round( total );
function round( num )
{
var newNum = Math.round( parseFloat( num ) * 100 ) / 100;
return ( isNaN( newNum ) ) ? 0 : newNum;
}
}
Stoffel
02-04-2003, 09:15 PM
ow ic
I don't think that would make a difference?
Stoffel
02-05-2003, 12:30 PM
Ow didn't see your message Beetle,
I'll try it when I'm home
joeframbach
02-05-2003, 12:44 PM
this sounds like homework to me...:eek:
Stoffel
02-05-2003, 02:41 PM
Nope it ain't :D
I am at my sisters home right now so I can't work at it
Stoffel
02-05-2003, 09:04 PM
Thnx beetle
it works great
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.