inktherapy Posted November 18, 2008 Share Posted November 18, 2008 Hi,I am making a bulk order price list, please see my code below. My code works fine but what I need is to round off the decimal numbers to 2 digits... e.g. if total price is 398.8999999 but I want to display it like 398.90 , thanks. <html><head><script type="text/javascript">function getIndex(){var x=document.getElementById("mySelect");var y = x.selectedIndex;var z = new Array();z[0] = 7.45;z[1] = 5.34;z[2] = 7.25;z[3] = 4.99;var a = document.getElementById("quantity").value;var b = z[y] * a;if (a == "" || isNaN(a)) {alert("Please enter quantity");} else {switch (y) {case 0: document.getElementById("display").innerHTML= "Coke " + "$" + b; document.getElementById("item").innerHTML= z[0];break;case 1: document.getElementById("display").innerHTML= "Royal " + "$" + b; document.getElementById("item").innerHTML= z[1];break;case 2: document.getElementById("display").innerHTML= "Sprite " + "$" + b; document.getElementById("item").innerHTML= z[2];break;case 3: document.getElementById("display").innerHTML= "Sarsi " + "$" + b; document.getElementById("item").innerHTML= z[3];}}}</script></head><body><form style="background-color:#CCCCCC; padding-left:10px; padding-top:10px;">Select your soda:<select id="mySelect" onclick="getIndex()"> <option>Coke</option> <option>Royal</option> <option>Sprite</option> <option>Sarsi</option></select><br />Item Price:<textarea id="item" cols="5" rows="1"></textarea><br />Quantity:<input type="text" id="quantity"/><br /><br /></form><div id="display" style="background-color:red; color:white; font-weight:bold; height:20px; padding-top:5px; padding-left:10px;">Total Price</div></body></html> Link to comment Share on other sites More sharing options...
Synook Posted November 18, 2008 Share Posted November 18, 2008 Math.round(n * 100) / 100 Link to comment Share on other sites More sharing options...
inktherapy Posted November 18, 2008 Author Share Posted November 18, 2008 Hi Synook,Thank you, I am not that good in arithmetic though but this is a wake up call! anyways I added your formula below (see code).I am planning to add "," if the value is in thousands, millions etc., I was thinking of .length that I read in w3school, but it's only basic, I am currently browsing the answer on web. <html><head><script type="text/javascript">function getIndex(){var x=document.getElementById("mySelect");var y = x.selectedIndex;var z = new Array();z[0] = 7.45;z[1] = 5.34;z[2] = 7.25;z[3] = 4.99;var a = document.getElementById("quantity").value;var b = z[y] * a;if (a == "" || isNaN(a)) {alert("Please enter quantity");} else {switch (y) {case 0: document.getElementById("display").innerHTML= "Coke " + "$" + Math.round(b*100) / 100; document.getElementById("item").innerHTML= z[0];break;case 1: document.getElementById("display").innerHTML= "Royal " + "$" + Math.round(b*100) / 100; document.getElementById("item").innerHTML= z[1];break;case 2: document.getElementById("display").innerHTML= "Sprite " + "$" + Math.round(b*100) / 100; document.getElementById("item").innerHTML= z[2];break;case 3: document.getElementById("display").innerHTML= "Sarsi " + "$" + Math.round(b*100) / 100; document.getElementById("item").innerHTML= z[3];}}}</script></head><body><form style="background-color:#CCCCCC; padding-left:10px; padding-top:10px;">Select your soda:<select id="mySelect" onclick="getIndex()"> <option>Coke</option> <option>Royal</option> <option>Sprite</option> <option>Sarsi</option></select><br />Item Price:<textarea id="item" cols="5" rows="1"></textarea><br />Quantity:<input type="text" id="quantity"/><br /><br /></form><div id="display" style="background-color:red; color:white; font-weight:bold; height:20px; padding-top:5px; padding-left:10px;">Total Price</div></body></html> Link to comment Share on other sites More sharing options...
Ache Posted November 18, 2008 Share Posted November 18, 2008 If you want it more extended you could use something like this: function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result;} And then: var roundedNumber = roundNumber(annualPremium,2) where you want / need it. Link to comment Share on other sites More sharing options...
inktherapy Posted November 18, 2008 Author Share Posted November 18, 2008 Hi Ache,Thank you very much, I will try and test this, I will let you know if I didn't get it right. Link to comment Share on other sites More sharing options...
jeffman Posted November 18, 2008 Share Posted November 18, 2008 I haven't read through your code, but it sounds like you maybe want the toFixed() method in there:http://www.w3schools.com/jsref/jsref_tofixed.asp Link to comment Share on other sites More sharing options...
inktherapy Posted November 18, 2008 Author Share Posted November 18, 2008 Sir, toFixed() method is convenient! Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.