乘法运算后,我希望输出为2位小数?怎么做



我想根据gien输入计算乘法。但是,我希望输出不超过2位小数。

例如,12.24和13.17的乘积为161.2008。我希望输出被截断为2位小数,即输出应该是161.20。

我不是一个专业的人,工作只是为了让这个项目发挥作用。所以,如果你们能编辑我的代码并教我,那也将不胜感激。

我在这里粘贴了我项目的部分代码:

function dbkcalc()
{

var decde = (document.getElementById('DECDAPR').value * document.getElementById('DECDAPC').value);
document.getElementById('DECDE').value= decde;
}
<style>
td
{
width: 50%;
border: 1px solid ORANGE;
}

</style>
<body>
<table align="center">

<tr>
<td>DECDAPR</td>
<td><input id="DECDAPR"></td>
</tr>
<tr>
<td>DECDAPC</td>
<td><input id="DECDAPC"></td>
</tr>
<tr>
<td colspan=2>
<input id="CALDIF" type="button" class="button" value="CALCULATE" onClick="dbkcalc()">
</td>
</tr>
<tr>

<td><U>DECDE</U></td>
<td class="declared"><U><output id="DECDE" type="number"></U></td>
</tr>
</table>
</body>

使用Math.round((方法:四舍五入2位小数:

var a = 12.24;
var b = 13.17;
var c = a * b;
c = Math.round(c * 100) / 100;
console.log(c);

如果您想截断而不是四舍五入,请使用toFixed((方法:

var a = 12.24;
var b = 13.17;
var c = a * b;
c = c.toFixed(2);
console.log(c);

这将截断为两位数字。

这里已经问过了。你真的应该试着在发布之前用谷歌搜索这些简单的东西。但无论如何,只需将1个javascript代码行更改为document.getElementById('DECDE').value = decde.toFixed(2);

最新更新