Since you're looking for 2 digits of precision, you want to multiply the number by 10^2
(100) and then round to the nearest integer by calling Math.round
. Then divide the result by the same factor you multiplied by (100).
If you want more digits of precision (after the decimal point), then just multiply/divide by larger powers of 10 (eg: 3 digits would be 1000, 4 would be 10000, etc).
var n = 2.74;
var result = Math.round((n / 4) * 100) / 100;
document.getElementById('output').innerHTML = (result * 3).toString() + '<br>' + (n - (result*3));
<div id="output"></div>
Another option as mentioned in the comments to your question would be to use toFixed
:
var n = 2.74;
var result = (n/4).toFixed(2) * 3;