Jump to content

Rounding price result to 2 decimal places


DarnStuckAgain

Recommended Posts

Apologies for asking this question but I can't make the solutions work for my specific example :/

 

Here is my calculation function and I'd like it to give me the area and the total price to 2 decimal places

 

 

function calculate(myForm) {

 

document.myForm.area.value = (document.myForm.length.value -0) * (document.myForm.height.value -0);

 

document.myForm.totalprice.value = (document.myForm.area.value -0) * (document.myForm.priceperunit.value -0);

 

}

Check this reall good.

I only did it on the fly and just a couple of numbers

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>The greatest page title ever</title>
<script src="./lib/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function calculate(myForm)
{
//document.getElementById('area').value = Number(document.getElementById('length').value);

document.getElementById('area').value = Number(document.getElementById('length').value) * Number(document.getElementById('height').value);
var interum = Number(document.getElementById('area').value) * Number(document.getElementById('priceperunit').value);
document.getElementById('totalprice').value = Math.round(interum*100)/100
}
</script>
</head>

<body>
<form id="myForm">

length :<input type="text" id="length" /><br />
height :<input type="text" id="height" /><br />
price  :<input type="text" id="priceperunit" /><br />
<input type="text" id="area" />area<br />
<input type="text" id="totalprice" />totalprice<br />
<input type="button" onclick="calculate('myForm')" />
</form>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.