带有HTML5输出的舍入小数点 [英] Round decimal point with HTML5 output

查看:67
本文介绍了带有HTML5输出的舍入小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个看起来像html5的计算器

I am building an html5 calculator that looks like

<form onsubmit="return false" oninput="o.value = a.valueAsNumber + b.valueAsNumber"<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>

并希望将结果四舍五入到小数点后两位.

And would like to round the results to 2 decimal points.

推荐答案

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

这篇关于带有HTML5输出的舍入小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆