round to 3 decimal points in javascript/jquery
Use num.toFixed(d)
to convert a number into the String representation of that number in base 10 with d
digits after the decimal point, in this case,
margin_total.toFixed(3);
The
toFixed()
method converts a number into a string, keeping a specified number of decimals. A string representation of input that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.
function myFunction() {
var num = 5.56789;
var n = num.toFixed(3)
document.getElementById("demo").innerHTML = n;
}
<p>Click the button to display the fixed number.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>