6

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>
2
  • 2
    Javascript is not the same as Java. I've edited your post to reflect this. Commented Nov 10, 2011 at 16:40
  • 1
    Jon Skeet has edited your question, consider yourself lucky! Commented Jun 16, 2014 at 21:13

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It helped me out eventually. =) I'm still new to this.
No problem. Remember @JonSkeet's advice when looking up JavaScript stuff. Java is not the same language as JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.