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>
math questions
Moderator: General Moderators
happens because of the internal representation of numbers should fix that.
The cast to string should be implicit but if you want to force it try obj.toString()
Code: Select all
<html>
<head>
<script language="javascript" type="text/javascript">
var totalcost=0;
function testmath()
{
totalcost+= 455.99;
document.getElementById("blah").value=totalcost.toFixed(2);
}
</script>
</head>
<body>
<input tpye="text" disabled="disabled" id="blah" />
<br/>
<a href="javascript:testmath()">click here</a>
</body>
</html>The cast to string should be implicit but if you want to force it try obj.toString()