Javascript Number round(precision)

Description

Javascript Number round(precision)


/**/*from w  ww .  ja  v  a2  s  .  co m*/
 * Rounds a number to a given precision and omits trailing 0 values.
 *
 * @example  100.51235.round(3)
 *           100.512
 *
 * @param    precision {Number} The level of procision, i.e. how many decimals
 *                              appear.
 *
 * @return   {Number} The given number, rounded to the given precision.
 */

Number.prototype.round = Number.prototype.round || function (precision) {
  return Math.round(this * Math.pow(10, (precision || 2))) / Math.pow(10, (precision || 2))
}

Javascript Number round(precision)

Number.prototype.round = function(precision) {
  var factor = Math.pow(10, precision || 0);
  return Math.round(this * factor) / factor;
};

Javascript Number round(precision)

Number.prototype.round = function(precision){
 if (Object.isUndefined(precision)){
  return Math.round(this);
 }
 
 precision = Math.pow(10, precision);
  
 return Math.round(this * precision) / precision;
};



PreviousNext

Related