dr1054 dr1054 - 2 months ago 19
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:

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


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.

Answer

Use toFixed() method like following.

$("#ThursdayDinnerFee").val((90).toFixed(2))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="ThursdayDinnerFee" type="text">

Comments