我想将结果四舍五入到小数点后两位。我已经尝试过 Math.floor*(Math.pow (1.02,7)*100)/100 但我得到的是 1,150 而不是 1,148.69 - 我打算返回的答案。我的代码片段 atm:function money (amount) { return amount*(Math.pow (1.02,7))}money(1000);
3 回答
慕姐8265434
TA贡献1813条经验 获得超2个赞
您可以使用toFixed舍入到某个小数
function money (amount) {
return (amount*(Math.pow (1.02,7))).toFixed(2)
}
console.log(money(1000))
撒科打诨
TA贡献1934条经验 获得超2个赞
function money (amount) {
return Math.round(((amount*(Math.pow (1.02,7))) * 100)) / 100;
}
console.log(money(1000));
这会给
繁星淼淼
TA贡献1775条经验 获得超11个赞
以下是.toFixed()使用方法:
function money(amount){
return (amount*Math.pow(1.02, 7)).toFixed(2);
}
console.log(money(1000));
添加回答
举报
0/150
提交
取消