HTML5 Number Input - Always show 2 decimal places

后端 未结 15 2161
太阳男子
太阳男子 2020-11-28 05:43

Is there\'s any way to format an input[type=\'number\'] value to always show 2 decimal places?

Example: I want to see 0.00 instead of

15条回答
  •  星月不相逢
    2020-11-28 06:33

    If you landed here just wondering how to limit to 2 decimal places I have a native javascript solution:

    Javascript:

    function limitDecimalPlaces(e, count) {
      if (e.target.value.indexOf('.') == -1) { return; }
      if ((e.target.value.length - e.target.value.indexOf('.')) > count) {
        e.target.value = parseFloat(e.target.value).toFixed(count);
      }
    }
    

    HTML:

    
    

    Note that this cannot AFAIK, defend against this chrome bug with the number input.

提交回复
热议问题