$("#result2").text(val1 / val2).toFixed(3);
There's your problem. You want to call toFixed(3)
on the value before you pass it to text()
.
$("#result2").text((val1 / val2).toFixed(3));
It may be useful to step through with a debugger at times like this, to see where expressions are evaluated and how it works.