四舍五入到小数点后两位 (Javascript)

我想将结果四舍五入到小数点后两位。我已经尝试过 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);


holdtom
浏览 91回答 3
3回答

慕姐8265434

您可以使用toFixed舍入到某个小数function money (amount) {  return (amount*(Math.pow (1.02,7))).toFixed(2)}console.log(money(1000))

撒科打诨

function money (amount) {    return  Math.round(((amount*(Math.pow (1.02,7))) * 100)) / 100;}console.log(money(1000));这会给

繁星淼淼

以下是.toFixed()使用方法:function money(amount){  return (amount*Math.pow(1.02, 7)).toFixed(2);}console.log(money(1000));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript