0

Possible Duplicate:
JavaScript: formatting number with exactly two decimals
Round up to 2 decimal places in javascript

<form name="shippingForm" method="post" action="">
<h2>Product Cost</h2>
<ul>
<li><input type="text" name="YourValue" id="YourValue" value=""/></li>
<li><label>VAT @ 17.5% +</label></li>
<li><input type="text" name="costShipping" id="costShipping" value=""/></li>
<li><label>Currency conversion rate: 1.98</label></li>
<li><div id="totalPrice"></div></li>
<li><input type="button" value="Calculate" name="calculate" id="calculate"/></li>
</ul>
</form>

Above is the form. What this does, is ask the user to enter 2 values, but the 2nd value is optional. It then calculates and displays the result.

<script type="text/javascript">
var button = document.getElementById('calculate');
button.onclick = function() {
var YourValue = parseInt(document.getElementById('YourValue').value);
var LocalTax = 17.5/100 * YourValue;
var costShipping = parseInt(document.getElementById('costShipping').value);
var convCurrency = 1.98;
var totalPrice = document.getElementById('totalPrice');

if (document.getElementById('costShipping').value === "") {
    totalPrice.innerHTML = "= $"+(YourValue + LocalTax + 0) * convCurrency;
}

else {
    totalPrice.innerHTML = "= $"+(YourValue + LocalTax + costShipping) * convCurrency;
}
};
</script>

what i would like to do is to round off the totalPrice result to 2 decimals

Community
  • 1
  • 1
J.Fenty
  • 11
  • 2

1 Answers1

0

Don't reinvent the wheel.

Take a look at accounting.js:-

http://josscrowcroft.github.com/accounting.js/

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42