# Extend Math.round, Math.ceil and Math.floor to allow for precision

DevFeed: [Extend Math.round, Math.ceil and Math.floor to allow for precision](<https://devfeed.tech/articles/extend-math-round-math-ceil-and-math-floor-to-allow-for-precision-51935.md>)

Original publisher: [Read original article](<https://lea.verou.me/2009/02/extend-mathround-mathceil-and-mathfloor-to-allow-precision/>)

Author: Lea Verou

Published: 2009-02-12T00:00:00Z

Content type: tutorial

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [Mathematics](<https://devfeed.tech/topics/mathematics.md>), [Closure](<https://devfeed.tech/topics/closure.md>), [bestpractice](<https://devfeed.tech/topics/bestpractice.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [blog](<https://devfeed.tech/tags/blog.md>), [closure](<https://devfeed.tech/tags/closure.md>), [code](<https://devfeed.tech/tags/code.md>), [conditional](<https://devfeed.tech/tags/conditional.md>), [dry](<https://devfeed.tech/tags/dry.md>), [foreach](<https://devfeed.tech/tags/foreach.md>), [function](<https://devfeed.tech/tags/function.md>), [functions](<https://devfeed.tech/tags/functions.md>), [js](<https://devfeed.tech/tags/js.md>), [math](<https://devfeed.tech/tags/math.md>), [original](<https://devfeed.tech/tags/original.md>), [performance](<https://devfeed.tech/tags/performance.md>), [precision](<https://devfeed.tech/tags/precision.md>), [prototype](<https://devfeed.tech/tags/prototype.md>), [tests](<https://devfeed.tech/tags/tests.md>), [variables](<https://devfeed.tech/tags/variables.md>)

## AI overview

The excerpt proposes extending Math.round, Math.ceil, and Math.floor with a precision argument by scaling the number by 10^precision, applying the saved native function, then scaling back. It says omitted or falsy precision defaults to zero, preserving the original integer behavior. To avoid duplicating wrapper code, it shows looping over the function names; a closure keeps loop variables out of the global namespace, while a forEach version is offered as a more readable alternative when available. The author reports the wrappers took about twice as long as native calls in their tests; a conditional fast path for zero precision offered little improvement and slowed calls with positive precision. The excerpt does not establish behavior for broader numeric edge cases.

## Source excerpt

Math.round, Math.ceil and Math.floor are very useful functions. However, when using them, I find myself many times needing to specify a precision level. You don't always want to round to an integer, you often just want to strip away some of the decimals. We probably all know that if we have a function to round to integers, we can round to X decimals by doing Math.round(num*Math.pow(10,X)) / Math.pow(10,X). This kind of duck typing can get tedious, so usually, you roll your own function to do that. However, why not just add that extra functionality to the functions that already exist and you're accustomed to? Let's start with Math.round. It's the most needed one anyway. Firstly we'll have to store the native function somewhere, since we're going to replace it. So we do something along the lines of: Math._round = Math.round; Now let's sigh replace the native Math.round with our own: Math.round = function(number, precision) { precision = Math.abs(parseInt(precision)) || 0; var coefficient = Math.pow(10, precision); return Math._round(number*coefficient)/coefficient; } And guess what? It still works the old way too, so your old scripts won't break. So now, let's go to Math.ceil and Math.floor. If you notice, the only thing that changes is the function name. Everything else is the same. So, even though we could copy-paste the code above and change the names, we would end up with triple the size of the code that we need and we would have also violated the DRY principle. So we could put the names of the functions in an array, and loop over it instead: (function(){ var MathFns = ['round', 'floor', 'ceil' ]; for(var i = MathFns.length; i>-1; i--) { Math['_' + MathFns[i]] = Math[MathFns[i]]; Math[MathFns[i]] = function(number, precision) { precision = Math.abs(parseInt(precision)) || 0; var coefficient = Math.pow(10, precision); return Math['_' + MathFns[i]](number*coefficient)/coefficient; } } })(); Why the closure? To allow us to be free in defining our variables without po