JSFiddle - React, Tailwind, and code Playground
by Antal Orcsik
JavaScript
function roundNumber(number, decimals) {
var d = parseInt(decimals,10),
dx = Math.pow(10,d),
n = parseFloat(number),
f = Math.round(Math.round(n * dx * 10) / 10) / dx;
return f.toFixed(d);
}
var v = 22.044960000000003;
alert(new Number(v+'').toFixed(2)); // 22.04
// alert(Math.round(Math.round(v * 1000) / 10) / 100); // 22.05
alert(roundNumber(v,2)); // 22.05
var v = 152.79499999999998;
alert(new Number(v+'').toFixed(2)); // 152.77
// alert(Math.round(Math.round(v * 1000) / 10) / 100); // 152.78
alert(roundNumber(v,2)); // 152.78