Mark Mark - 2 months ago 29
Javascript Question

Numeric Textbox - Formatting

I have a disabled textbox that shows a calculated percentage.
I am looking to format this text box to always show a single decimal place, even if the number is a whole number.

I feel like this should be simple, but for some reason I can't figure this out.

Any suggestions?

<div class="labelReallySmall">
@Html.Label("%F", null, new { @class = "req" })
@Html.TextBox("PFat305", null, new { disabled = true })
<script>
$(document).ready(function () {
$("#305Fat").change(function () {
var a = parseInt($(this).val());
var b = parseInt($("#Milk305").val());
var c = (a*100)/b ;
$("#PFat305").val(c);
});
});
</script>
</div>


Thanks in advance, Mark

Answer

Try .toFixed(n) - where n - is number of decimals.

    alert((10.82).toFixed(1));