DarnStuckAgain Posted April 9, 2012 Share Posted April 9, 2012 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 https://forums.phpfreaks.com/topic/260623-rounding-price-result-to-2-decimal-places/ Share on other sites More sharing options...
sunfighter Posted April 10, 2012 Share Posted April 10, 2012 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 https://forums.phpfreaks.com/topic/260623-rounding-price-result-to-2-decimal-places/#findComment-1335908 Share on other sites More sharing options...
caliux Posted April 10, 2012 Share Posted April 10, 2012 This you should have learned at Math Javascript.Try that number.toFixed(2); Link to comment https://forums.phpfreaks.com/topic/260623-rounding-price-result-to-2-decimal-places/#findComment-1335965 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.