Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Decimals in Javasrcipt

Status
Not open for further replies.

285

Programmer
Mar 11, 2008
2
GB
Is there a way to limit the decimal points in this script?

<HTML>
<HEAD>
<TITLE>Calculator</TITLE>

<SCRIPT LANGUAGE="JavaScript">
function CalculateSum(Atext, Btext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
form.Answer.value = A/(B*B);
}
</SCRIPT>
</HEAD>

<BODY>

<P><FONT SIZE="+2">Calculator</FONT></P>

<FORM NAME="Calculator" METHOD="post">
<P>Text A: <INPUT TYPE=TEXT NAME="input_A" SIZE=5></P>
<P>Text B: <INPUT TYPE=TEXT NAME="input_B" SIZE=5></P>
<P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onClick="CalculateSum(input_A.value,input_B.value, this.form)"></P>
<P><input type="reset" name="reset" value="Reset" id="reset" />
<P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P>
</FORM>

</BODY>
</HTML>
 
Like this:
Code:
form.Answer.value = (A/(B*B)).toFixed(numdecimalplaces);

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top