One of the things that bothers me about the native rounding functions is the fact that there is no way to specify precision. If this is something that you need to do you can use the following code to define `Math.\$round(...)`, `Math.\$ceil(...)`, and `Math.\$floor(...)`:

``````
(function(arr,i,name) {
while(name = arr[i++]) {
Math["\$"+name] = Function("a","b","return Math."+name+"(a*(b=Math.pow(10,b||0)))/b");
}
})(["floor","ceil","round"],0);
``````

This code Makes the following functions available:

• `Math.\$ceil(number [, precision])` → `Number`:
Returns the specified number rounded up just as `Math.ceil(number)` does, but also gives the ability to specify the precision of the rounded number.

• `Math.\$floor(number [, precision])` → `Number`:
Returns the specified number rounded down just as `Math.floor(number)` does, but also gives the ability to specify the precision of the rounded number.

• `Math.\$round(number [, precision])` → `Number`:
Returns the specified number rounded up or down just as `Math.round(number)` does, but also gives the ability to specify the precision of the rounded number.

Specifying a positive precision will round according to that digit to the right of the decimal point. Specifying a negative precision will round according to that digit to the left of the decimal point.

Categories: BlogJavaScriptJScriptMath #### ildar · November 9, 2012 at 12:30 PM

Hmmm. Number object has these methods

Math.PI.toFixed(3)
Math.PI.toExponential(3)
Math.PI.toPrecision(3) #### ildar · May 17, 2013 at 3:57 AM

The MDN pages have the example the similar to your solution – https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/ceil. I decided to share them here. The main difference is in usage of parsing of a string presentation of numbers against exponentiation. Maybe the more common solution is a modulo adjustment like this – http://with-love-from-siberia.blogspot.ru/2013/05/modulo-adjustment.html 🙂 #### rJbueno · August 20, 2013 at 2:02 PM

Lifesaver, thanks a mill!!! #### miguel guerrero · January 16, 2014 at 1:44 PM

And the examples??? #### Chris West · February 10, 2014 at 5:19 PM

I used Chrome’s JS console and wrote the following quick example:
[code language="javascript"]
var funcs = ['\$ceil', '\$floor', '\$round'];
var arg2 = [-1, 0, 1];
funcs.map(function(fn) {
return arg2.map(function(arg2) {
return 'Math.' + fn + '(45.67, ' + arg2 + ') -> ' + Math[fn](45.67, arg2);
}).join('\n');
}).join('\n---\n');
[/code]

This results in the following:
[code language="plain"]
Math.\$ceil(45.67, -1) -> 50
Math.\$ceil(45.67, 0) -> 46
Math.\$ceil(45.67, 1) -> 45.7
---
Math.\$floor(45.67, -1) -> 40
Math.\$floor(45.67, 0) -> 45
Math.\$floor(45.67, 1) -> 45.6
---
Math.\$round(45.67, -1) -> 50
Math.\$round(45.67, 0) -> 46
Math.\$round(45.67, 1) -> 45.7
[/code] #### Justin James · July 11, 2014 at 11:54 AM

Awesome. Thanks Chris!!!! #### Travis · November 19, 2015 at 11:29 PM

This is great, however at least in chrome the following examples do not work:
Math.\$round(35.855,2) -> 35.85
Math.\$round(1.005,2)->1
Apparently this is because of Javascript’s handling of floating point.
You can read more here :
http://stackoverflow.com/questions/10015027/javascript-tofixed-not-rounding