html - 使用 html 和 javascript 将结果四舍五入到小数点后 2 位
问题描述
<script>
function e(element)
{
return document.getElementById(element);
}
function f(element)
{
return parseFloat(e(element).value);
}
function updateResult()
{
e("inputK").value = f("inputG")*1.33* (f("inputI")/(f("inputH")+ f("inputI")))
}
</script>
对于上面显示的代码,我请求帮助将值四舍五入到小数点后 2 位
结果-- input type="text" id="inputK" readonly="true" 我试过 math.round(inputK) 它不起作用你的建议将帮助我改善结果。
解决方案
正如您所提到的,您正在使用仅返回整数的Math.round() 。有多种方法可以实现目标。
Math.round(num * 100) / 100
-- 返回数字var numb = 123.23454; numb = numb.toFixed(2);
-- 返回字符串Math.round(num + "e+2") + "e-2"
-- 返回数字Number.prototype.round = function(places) { return +(Math.round(this + "e+" + places) + "e-" + places); }
-- 返回数字
推荐阅读
- r - 数据 [[y_orig_val]] 中的错误:下标越界 - 从插入符号包运行 train() 时
- javascript - javascript - 从字符串中获取参数
- c - AddressSanitizer 中的“影子字节”是什么,我应该如何解释它们?
- java - 如何在 Jmeter 中使用 Java 加载公钥?
- reactjs - React - 将组件作为参数传递给另一个组件
- reactjs - 如何在 useState() 中将函数作为对象的初始值传递?
- android - 将现有的 WebSocket 实现转换为 Android Java 中的 Reactive WebSocket
- database - 配置单元表存储在 Azure 存储帐户中的什么位置?
- r - 在 R 中添加带有 % 的新列
- python - “return”关键字有什么作用?