In jQuery, what's the best way of formatting a number to 2 decimal places?

前端 未结 3 1021
难免孤独
难免孤独 2020-11-27 13:59

This is what I have right now:

$(\"#number\").val(parseFloat($(\"#number\").val()).toFixed(2));

It looks messy to me. I don\'t think I\'m

3条回答
  •  无人及你
    2020-11-27 14:32

    If you're doing this to several fields, or doing it quite often, then perhaps a plugin is the answer.
    Here's the beginnings of a jQuery plugin that formats the value of a field to two decimal places.
    It is triggered by the onchange event of the field. You may want something different.

       
    

提交回复
热议问题