how to round using ROUND HALF UP in javascript? I am using Prototype JavaScript framework version 1.5.1_rc3, so I prefer to use it if available. If not, I appreciate also if you share it.
Any guidance is appreciated.
how to round using ROUND HALF UP in javascript? I am using Prototype JavaScript framework version 1.5.1_rc3, so I prefer to use it if available. If not, I appreciate also if you share it.
Any guidance is appreciated.
The Math.round() will round up when n >= 5
else round down
Examples:
Math.round(20.49);// 20
Math.round(20.5);// 21
Math.round(-20.5);// -20
Math.round(-20.51);// -21
Note: The examples were taken from the link above
Native functions don't bite, my advice is to try to always try to use them whenever possible. They are faster and do the same job after all
Consider:
if (!Number.prototype.round) {
Number.prototype.round = function() {
return Math.round(this);
}
}
var x = 5.49;
alert(x.round()); // 5
alert((7.499).round()); // 7