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);

 

}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.