I create a small script for calculate the amount of water a certain size of content can hold. But i can't figure what to add to my script for round the decimal to 2 max. Here à copie of the working script and html code, please show me what to modify to limit the amount of decimal. Thank you!
Maybe this helps <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script type="text/javascript"> function round2(num) { var roundnum = 2;// up to 2 decimal numbers var rstr = ''; var decstr = ''; var numstr = num + ''; var iod = numstr.indexOf('.'); var i = 0; var tail = ''; if (iod > 0) {// with decimal rstr = numstr.substring(0, iod); decstr = numstr.substring(iod + 1, numstr.length); while (tail.length < roundnum) { if (decstr.charAt(i)) { tail += decstr.charAt(i); } else { tail += "0"; } i++; } decstr = tail; } else {// no decimal rstr = numstr; while (decstr.length < roundnum) { decstr += "0"; } } return(rstr + "." + decstr); } </script> </head> <body> <input type="button" value="Check" onclick="alert(round2(3.213))" /> </body> </html> HTML:
I don't understand how to incorporate the round calculation in my original script for have a result with only 2 decimal. Can you copy past my script and add the line for make it work?
Sorry, no, i'm not a pro in javascript. I just start understanding this JS ....Where i exactly paste the code in my html page for make it work??? I put here the complete code of the html page, show m where to copy the lines. Thanks for help. <html> <head> <script language="javascript"> function calc (form) { form.liter.value = form.length.value * form.width.value * form.height.value *0.016387064069264; form.gallon.value = form.liter.value /100 *26.4172; } </script> </head> <body> <form> length: <input type="text" class="but" name="length" size="2" maxlength="3"><br> width: <input type="text" class="but" name="width" size="2" maxlength="3"><br> height: <input type="text" class="but" name="height" size="2" maxlength="3"><br><br> <input type="button" class="bouton" value="GO" onclick="calc(this.form)"> <br><br> Liters: <input type="text" SIZE="8" value="" name="liter"><br> Gallon: <input type="text" SIZE="8" value="" name="gallon"><br> </form> </body> </html> Code (markup):
Change this: <script language="javascript"> function calc (form) { form.liter.value = form.length.value * form.width.value * form.height.value *0.016387064069264; form.gallon.value = form.liter.value /100 *26.4172; } </script> Code (markup): To this: <script language="javascript"> function calc (form) { var Liters = form.length.value * form.width.value * form.height.value * 0.016387064069264; var Gallons = Liters * 0.264172; form.liter.value = Liters.toFixed(2); form.gallon.value = Gallons.toFixed(2); } </script> Code (markup):