To round a decimal number with the .toFixed()
method in JavaScript, I would expect that the ending of ...5 should be rounded up.
As the example shows, in the case of 5.555, the .toFixed(2)
method will result in 5.55 although my expectation would be 5.56 due to the rounding standards.
Is any built-in JavaScript or jQuery method for rounding numbers following the international mathematical rounding?
Edit: Using Math.round()
is acceptable and understandable, just the number shall be multiplied and divided. I was curious if there is any existing simplified function, like the toFix()
just with a solution on the rounding of 5's. Of course it possible to write a function for it or use the Math.round with proper parameterization.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the fixed number.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var num = 5.555;
var n = num.toFixed(2);
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>