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

前端 未结 3 1014
难免孤独
难免孤独 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:47

    Maybe something like this, where you could select more than one element if you'd like?

    $("#number").each(function(){
        $(this).val(parseFloat($(this).val()).toFixed(2));
    });
    

提交回复
热议问题