Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  75] [ 1]  / answers: 1 / hits: 17939  / 13 Years ago, thu, november 10, 2011, 12:00:00

This is my Code so far. I want to know how to return the answer with two decimal places. EX. Instead of 515.3 I want 515.30 or instead of 500 I want it to return 500.00.



<HTML>
<HEAD>
<TITLE>Simple Adder</TITLE>
<SCRIPT LANGUAGE=JavaScript>
function CalculateSum(Atext, form)
{
var A = parseFloat(Atext);
form.Answer.value = A * .03+.30+A;
}
function ClearForm(form)
{
form.input_A.value = ;
form.Answer.value = ;
}

// end of JavaScript functions -->
</SCRIPT>
</HEAD>

<BODY>

<P><FONT SIZE=+2>Credit Card Payment Calculator</FONT></P>

<FORM NAME=Calculator METHOD=post>
<P>Enter Base Rent: <INPUT TYPE=TEXT NAME=input_A SIZE=10></P>
<P><INPUT TYPE=button VALUE=Calculate Rent & Fees Payment name=AddButton onClick=CalculateSum(this.form.input_A.value, this.form)></P>
<P><INPUT TYPE=button VALUE=Clear Fields name=ClearButton onClick=ClearForm(this.form)></P>
<P>Final Amount = <INPUT TYPE=TEXT NAME=Answer SIZE=12></P>
</FORM>

</BODY>
</HTML>

More From » decimal

 Answers
54

Use Number.prototype.toFixed. For example:



var a = 515.3;
alert(a.toFixed(2));


Note that this converts the number to a string so only use it to display to the user.


[#89201] Wednesday, November 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;