How will i round margin_total to 3 decimal places?
margin_total = margin_total + parseFloat(marginObj.value);
document.getElementById('margin_total').value = margin_total;
How will i round margin_total to 3 decimal places?
margin_total = margin_total + parseFloat(marginObj.value);
document.getElementById('margin_total').value = margin_total;
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>
const num = 70.012345678900
console.log(parseFloat(num.toFixed(3)));
// expected output: 70.012
const roundedDown = Math.round(6.426475 * 1000) / 1000
console.log(roundedDown)
const roundedUp = Math.round(6.426575 * 1000) / 1000
console.log(roundedUp)
The above code rounds to 3 decimal places (dp)
To round to 2 dp use 100 instead of 1000 (all occurrences)
To round to 4 dp use 10000 instead of 1000
You get the idea!