Was testing toFixed() in 'Tryit' , basically want to be able to input the number of decimal places to display in the output. Have been using this with "toExponential" in many pages with no issues, but seemed not to work with "toFixed". The code below can be swapped by changing the comment lines to test, so either you ask for the number of dp or it can be set by "var decPlaces". However now results seem erratic, it may or may not work in either method - the dp may be shown or it gets rounded to 6 and sometimes it work for the input dp or the declared dp??
Also does 'var num' not have to be declared as a 'new Number' to get the '.toXXX' methods or does declaring var = 1; automatically create a number object? Its like the dec places is sometimes being picked up as a number and sometimes not.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the fixed number.</p>
<input type="number" id="input99" size="5" value="2" /> // input number of decimal places
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var decPlaces = document.getElementById("input99");
// var decPlaces = 4;
var num = 5.56789;
var n = num.toFixed(decPlaces);
// var n = num.toExponential(decplaces);
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>