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.