JSFiddle - React, Tailwind, and code Playground
HTML
<table>
<thead>
<tr>
<th>value</th>
<th>inprecise_round</th>
<th>precise_round</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
CSS
th,td{ padding: 0 10px;}
JavaScript
function inprecise_round(value, decPlaces) {
return Math.round(value*Math.pow(10,decPlaces))/Math.pow(10,decPlaces);
}
function precise_round(num,decimals) {
var sign = num >= 0 ? 1 : -1;
return (Math.round((num*Math.pow(10,decimals)) + (sign*0.001)) / Math.pow(10,decimals)).toFixed(decimals);
}
function writeVal(val, precision)
{
$('tbody').append('<tr><td>' + val + ' :: ' + precision + '</td><td>' + inprecise_round(val, precision) + '</td><td>' + precise_round (val, precision) + '</td></tr>');
}
writeVal(342.5555, 0)
writeVal(-342.55555, 0)
writeVal('1.2345678', 5);
writeVal('1.2345', 2);