0

Im trying to round a number to 2 decimal place. I have tried the following but not having any luck? Can somebody please help me and tell me where im going wrong??

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

i search and tried this but to be honest i have no idea what it is?

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

2 Answers2

1

Try to use the following syntax instead and alter it to your needs

var num = 5.1;

num.toFixed(2); //will become 5.10
Mureinik
  • 252,575
  • 45
  • 248
  • 283
natali
  • 90
  • 1
  • 10
  • var winPercentage = totalWins/(totalWins+totalLost)*100;winPercentage.toFixed(2); document.getElementById('win-percentage').innerHTML = winPercentage + " %"; – heady12 Mar 04 '15 at 20:52
  • var winPercentage = totalWins/(totalWins+totalLost)*100;winPercentage.toFixed(2); document.getElementById('win-percentage').innerHTML = winPercentage + " %"; – heady12 Mar 04 '15 at 20:52
0

You had the right idea with toFixed(2). The problem is that it returns the formatted number, it does not alter the variable it was called on. In other words, you just need to assign it back the the variable you were using:

winPercentage = winPercentage.toFixed(2);
Mureinik
  • 252,575
  • 45
  • 248
  • 283