//*** This code is copyright 2003 by Gavin Kistner, !@phrogz.net //*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt //*** Reuse or modification is free provided you abide by the terms of that license. //*** (Including the first two lines above in your source code satisfies the conditions.) //Rounds a number to a specific number of decimal places //Supports 'negative' decimals, e.g. // myNumber.roundToDecimals(-3) //will round to the nearest thousand // !!!!!!! Does not work properly on numbers -10?"."+n.substr(breakPoint):(Math.pow(10,-decimals)+"").substr(1))).replace(/^0+/,'0'); } //var x = 1234.3567 //x.roundTo(2); // --> 1234.36 //x.roundTo(-3); // --> 1000 //If you don't care about negative decimals, //AND you can guarantee that the result will not be less than 0.1 //the following version is about 15% faster: Number.prototype.roundTo=function(decimals){ var s=Math.round(this.valueOf()*Math.pow(10,decimals))+''; var chopPoint = s.length-decimals; return s.substr(0,chopPoint)+'.'+s.substr(chopPoint); }