且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有HTML5输出的舍入小数点

更新时间:2023-12-03 16:27:22

toFixed 是实现您要查找的内容的另一种方法,并且始终显示小数位,无论它们是否为0.

toFixed is another way to achieve what you are looking for, and will always show the decimal places regardless if they are 0 or not.

<form onsubmit="return false" oninput="o.value = (a.valueAsNumber + b.valueAsNumber).toFixed(2)"<br>
    <input name="a" id="a" type="number" step="any"> +<br>
    <input name="b" id="b" type="number" step="any"> =<br>
    <output name="o" for="a b"></output><br>
</form>​

实时演示

Live Demo