math questions

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

math questions

Post 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>
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

can some one tell me how to convert a variable to a string, thnx
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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()
Post Reply