admin管理员组文章数量:1312747
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.
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.
Share Improve this question edited Jun 17, 2012 at 0:37 Joseph 120k30 gold badges184 silver badges237 bronze badges asked Jun 17, 2012 at 0:33 RLeisingerRLeisinger 2182 gold badges3 silver badges10 bronze badges 1-
Hmm,
<output>
tag. Interesting... – Joseph Commented Jun 17, 2012 at 0:38
2 Answers
Reset to default 4toFixed 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
This will round to two decimal places.
Math.round(num * 100)/100
This is a more general version of it, rounds to a set number of places.
function roundNumber(num, places) {
return Math.round(num * Math.pow(10, places)) / Math.pow(10, places);
}
roundNumber(24.2424, 2)
24.24
本文标签: javascriptRound decimal point with HTML5 outputStack Overflow
版权声明:本文标题:javascript - Round decimal point with HTML5 output - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741908192a2404276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论