add limitSigDigs() function to number's prototype. tests included

git-svn-id: http://svn.openlayers.org/trunk/openlayers@1008 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-07-26 12:22:12 +00:00
parent 317c15a5ce
commit ce69912782
2 changed files with 44 additions and 1 deletions

View File

@@ -707,6 +707,22 @@ Array.prototype.indexOf = function(element) {
return index;
}
/**
* @param {int} sig
*
* @returns The number, rounded to the specified number of significant digits.
* If null, 0, or negaive value passed in, returns 0
* @type int
*/
Number.prototype.limitSigDigs = function(sig) {
var number = (sig > 0) ? this.toString() : 0;
if (sig < number.length) {
var exp = number.length - sig;
number = Math.round( this / Math.pow(10, exp)) * Math.pow(10, exp);
}
return parseInt(number);
}
/**
* @param {String} id
* @param {OpenLayers.Pixel} px