如何将数字四舍五入到小数点后两位? javascript

how to round a number to 2 decimal place? javascript

我正在尝试将数字四舍五入到小数点后两位。我尝试了以下但没有任何运气?有人可以帮助我并告诉我哪里出了问题吗??

var winPercentage = totalWins/(totalWins+totalLost)*100;
winPercentage.toFixed(2);
document.getElementById('win-percentage').innerHTML = winPercentage + " %";

我搜索并尝试了这个,但老实说我不知道​​它是什么?

var winPercentage = totalWins/(totalWins+totalLost)*100;
expr {double(round(100*winPercentage))/100}
document.getElementById('win-percentage').innerHTML = winPercentage + " %";

尝试使用以下语法并根据您的需要进行更改

var num = 5.1;

num.toFixed(2); //will become 5.10

您对 toFixed(2) 的想法是正确的。问题是它 returns 格式化数字 ,它不会改变调用它的变量。换句话说,你只需要将它分配回你正在使用的变量:

winPercentage = winPercentage.toFixed(2);