Page 1 of 1

math questions

Posted: Tue Apr 15, 2003 11:49 am
by nincha
im new at javascript and i got two questions;

1) can some one give me the syntax to round a number to two numbers before the decimal.

2) can some one tell me why $totalcost becomes all crazy after reaching 2500?

<html>
<script language="javascript" type="text/javascript">
function testmath()
{
$totalcost=$totalcost+455.99;
blah.innerHTML=$totalcost;
}
$totalcost=0;
</script>
<body>
<font id="blah"></font>
<br><a href="#" onClick="testmath();return false">click here</a>
</body>
</html>

Posted: Tue Apr 15, 2003 12:05 pm
by nincha
can some one tell me how to convert a variable to a string, thnx

Posted: Tue Apr 15, 2003 1:03 pm
by volka
happens because of the internal representation of numbers

Code: Select all

<html>
	<head>
		<script language="javascript" type="text/javascript">
			var totalcost=0;
			function testmath()
			&#123;
				totalcost+= 455.99;
				document.getElementById("blah").value=totalcost.toFixed(2);
			&#125;
		</script>
	</head>
	<body>
		<input tpye="text" disabled="disabled" id="blah" />
		<br/>
		<a href="javascript:testmath()">click here</a>
	</body>
</html>
should fix that.
The cast to string should be implicit but if you want to force it try obj.toString()