开发者

Is it ok to overwrite the default Math.round javascript functionality?

开发者 https://www.devze.com 2023-02-18 17:21 出处:网络
I have no need to use the Math.round functionality if it does not allow me to choose how many decimal p开发者_StackOverflow社区laces I want. So I have created the following function that I use instead

I have no need to use the Math.round functionality if it does not allow me to choose how many decimal p开发者_StackOverflow社区laces I want. So I have created the following function that I use instead.

Number.prototype.round = function(precision) {
    var numPrecision = (!precision) ? 0 : parseInt(precision, 10);
    var roundedNum = Math.round(this * Math.pow(10, numPrecision)) / Math.pow(10, numPrecision);
    return roundedNum;
};

My question is, can I change it to the following instead without any repercussions.

Math.roundP = function(num, precision){
   var pow = Math.pow(10, precision||0);
   return (Math.round(num*pow) / pow);
};

I realize that this will overwrite the default Math.round functionality, but I have no need for it. Is this okay in Javascript? I have not done this before so I just wanted to see what peoples thoughts on this is. Or maybe its better for me to leave it the way it is.

I am having trouble deciding when to use Number.prototype, and when to use Math.


You could, but I would strongly advise against it. It will obviously break any third-party code depending on the standard functionality.

The particular code you posted also has infinite recursion. You would need to store the original Math.round. But this again shows why not to mess with the standard functions. We all write bugs, but it's best to keep them confined to our code.


I'm getting a recursive error when I try your function, but the short of it is that it's fine. As long as you're defaulting the precision to zero, meaning that if the 2nd argument is not passed it will act identical to the original function, you won't effect anything.

For best practice, however, it's best to just call it something else.

FYI, my version:

Math.roundP = function(num, precision){
   var pow = Math.pow(10, precision||0);
   return (Math.round(num*pow) / pow);
};


I've used this method before

    Math.round = function(number, precision)
{
    precision = Math.abs(parseInt(precision)) || 0;
    var coefficient = Math.pow(10, precision);
    return Math._round(number*coefficient)/coefficient;
}

http://leaverou.me/2009/02/extend-mathround-mathceil-and-mathfloor-to-allow-precision/


I found this: http://www.mredkj.com/javascript/nfbasic2.html

Which allows you to just say: num.toPrecision(n) and you are done.

The link explains more.

Hope that helps.


If you are 100% certain that you have no use for Math.round ANYWHERE else, however it is good practice to not make this assumption. Monkey patching such a common function like this can almost turn out to be good.

External libraries that you use may very well use this method, which will screw things up.

0

精彩评论

暂无评论...
验证码 换一张
取 消