06 Feb
by: Matt in Development, JavaScript
tags: JavaScript, prototype, round, rounding
This very simple function does exactly what the title suggests, it allows you to round to any specified accuracy.
function roundTo(number, to) {
return Math.round(number * to) / to;
}
alert(roundTo(1532, 100)); // 1500
alert(roundTo(26, 10)); // 30
If you want to get fancy, you could also modify the Number prototype for a more "integrated" solution.
Number.prototype.roundTo = function(to) {
return Math.round(this * to) / to;
}
Related posts:
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.