1

I have no need to use the Math.round functionality if it does not allow me to choose how many decimal places 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.

Metropolis
  • 6,542
  • 19
  • 56
  • 86

5 Answers5

3

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • Good point...Thanks! So when is a good time to use Math and when is a good time to use Number? Should I use Number whenever there is already a function for what I am doing in Math? – Metropolis Mar 22 '11 at 03:07
  • @Metropolis, I would recommend being very wary about putting any methods in any of the core objects. – Matthew Flaschen Mar 22 '11 at 03:10
  • I am....I hardly use them, I have 6 functions maybe? Beyond that I have not come across any others that I need to put in here. But for the ones I DO have, I would like them to be in the best possible place. – Metropolis Mar 22 '11 at 03:11
  • Yes you are right about the recursion, I missed that. I did not even check it (or try it). I was really just trying to find an example of a situation that I was wondering about. – Metropolis Mar 22 '11 at 03:14
  • My little computer science knowledge tells me to avoid over riding the JS engine, better do your own implementation. That's the only reason prototype.js is known to be bad JS toolkit. +1 to @Matthew for reminding. – Kumar Mar 22 '11 at 03:24
2

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);
};
1

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/

sep15ms
  • 879
  • 1
  • 8
  • 6
  • I am going to use this in the example (since I set it up with infinite recursion). Thanks! – Metropolis Mar 22 '11 at 03:15
  • Yea, you might want to avoid that parseInt that was included in the link/example, or at least be careful using it by changing it to be base 10 http://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bug – sep15ms Mar 22 '11 at 03:58
  • Yeah I changed it to be cwolves version. I really was just showing an example, did not expect it to work. – Metropolis Mar 22 '11 at 14:06
1

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.

0

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.

Mike Lewis
  • 63,433
  • 20
  • 141
  • 111