wipe Posted November 24, 2009 Share Posted November 24, 2009 Hi guys, I have a small adder script to calculate some options. The problem is when i have as result (12.123) id like to limit to only (12.12), two numbers after (.). Here is the code i need to return only .00 <HTML> <HEAD> <TITLE>Adder</TITLE> <SCRIPT LANGUAGE="JavaScript"> function CalculateSum(Atext, Btext, form) { var A = parseFloat(Atext); var B = parseFloat(Btext); form.Answer.value = A * B; } function CalculateSum2(inp, form) { var C = parseFloat(inp); form.pp125.value = C * 16; form.pp285.value = C * 24; form.vsl.value = C * 19; form.vcl.value = C * 25; form.ppa.value = C * 34; var num = form.pp125.value; form.pp125.value.toFixed(2); } function ClearForm(form) { form.input_A.value = ""; form.input_B.value = ""; form.Answer.value = ""; } // end of JavaScript functions --> </SCRIPT> <style type="text/css"> <!-- .caixas { font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; background-color: #FFFFFF; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; color: #000000; } .caixaslinhas { font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; background-color: #FFFFFF; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-top-style: none; border-right-style: none; border-bottom-style: solid; border-left-style: none; color: #000000; border-bottom-color: #666666; } --> </style> </HEAD> <BODY> <P><FONT SIZE="+2">Calcular o espaço </FONT></P> <br> <table width="1271" border="0" cellpadding="0" cellspacing="0"> <tr> <td><FORM NAME="Calculator" METHOD="post"> <table width="398" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="125"> </td> <td width="253"> </td> <td width="20"> </td> </tr> <tr> <td>W:</td> <td><INPUT NAME="input_A" TYPE=TEXT class="caixaslinhas" SIZE=5 maxlength="5" STYLE="text-align: right;"> m</td> <td> </td> </tr> <tr> <td>H: </td> <td><INPUT NAME="input_B" TYPE=TEXT class="caixaslinhas" SIZE=5 maxlength="5" STYLE="text-align: right;"> m </td> <td> </td> </tr> <tr> <td>Space </td> <td><INPUT NAME="Answer" TYPE=TEXT disabled="disabled" class="caixas" SIZE=2 STYLE="text-align: right;"> m2</td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> <P> <INPUT TYPE="button" VALUE="Caculate" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form)"> <input type="button" value="Clear" name="ClearButton" onClick="ClearForm(this.form)"> </P> <P> </P> </FORM></td> <td><FORM NAME="Calculator2" METHOD="post"> <table width="398" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="125"> </td> <td width="253"> </td> <td width="20"> </td> </tr> <tr> <td>space:</td> <td><INPUT NAME="inp" TYPE=TEXT class="caixaslinhas" id="inp" STYLE="text-align: right;" SIZE=10> m</td> <td> </td> </tr> <tr> <td>125gr: </td> <td><INPUT NAME="pp125" TYPE=TEXT disabled="disabled" id="pp125" SIZE=10 maxlength="8" STYLE="text-align: right;"> €</td> <td> </td> </tr> <tr> <td>285gr: </td> <td><INPUT NAME="pp285" TYPE=TEXT class="caixas" id="pp285" SIZE=8 maxlength="8" rdisabled="disabled" STYLE="text-align: right;"> €</td> <td> </td> </tr> <tr> <td>SL </td> <td><INPUT NAME="vsl" TYPE=TEXT disabled="disabled" class="caixas" id="vsl" SIZE=8 maxlength="8" STYLE="text-align: right;"> €</td> <td> </td> </tr> <tr> <td>CL </td> <td><INPUT NAME="vcl" TYPE=TEXT disabled="disabled" id="vcl" SIZE=10 maxlength="8" STYLE="text-align: right;"> €</td> <td> </td> </tr> <tr> <td>TA </td> <td><INPUT NAME="ppa" TYPE=TEXT disabled="disabled" id="ppa" SIZE=10 maxlength="8" STYLE="text-align: right;"> €</td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> <P> <INPUT TYPE="button" VALUE="Calculate" name="AddButton2" onClick="CalculateSum2(this.form.inp.value, this.form)"> <input type="button" value="Limpar" name="ClearButton2" onClick="ClearForm(this.form)"> </P> <P> </P> </FORM></td> </tr> </table> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/182797-fix-to-two-decimal-numbers/ Share on other sites More sharing options...
KevinM1 Posted November 24, 2009 Share Posted November 24, 2009 Run the value through toFixed. I.e., var result = /*something */; alert(result.toFixed(2)); Just keep in mind that toFixed rounds up the decimal portion. Quote Link to comment https://forums.phpfreaks.com/topic/182797-fix-to-two-decimal-numbers/#findComment-964863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.