dr1054 dr1054 - 7 months ago 82
jQuery Question

Formatting number for 2 decimals - jquery

I have the following jquery function that auto populates a textbox based on the selection of a radio button option:

$(document).ready(function() {
$("#DinnerYes").click(function() {
$("#ThursdayDinnerFee").attr("value", 90);
$(document).ready(function() {
$("#DinnerNo").click(function() {
$("#ThursdayDinnerFee").attr("value", 0);

The script works and it auto populates the textbox (ThursdayDinnerFee), but I want the numbers to appear in the textbox as 0.00 and 90.00. I also need to keep the format as number because they will be used in a credit card transaction. If someone could help, I would greatly appreciate it. Thank you.


Use toFixed() method like following.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="ThursdayDinnerFee" type="text">