I have tried numerous scripts after 1 hour and half of googling and all have either given me the wrong rounded number or the wrong value altogether. These are two scripts I tried.
FIRST TRY:
Number.prototype.round = function(p) {
p = p || 10;
return parseFloat( this.toFixed(p) );
};
SECOND TRY:
function roundNumber(number, decimals) {
var newnumber = new Number(number+'').toFixed(parseInt(decimals));
return parseFloat(newnumber);
}
Some Example Outputs that I get from both:
0.22 -> 0.21480000000000005 (bad) should be 21
0.43 -> 0.4284 (good) 43 is right
What am I doing wrong? Any help would be appreciated as this has had me poking for too long.