Round implementation - Node.js Math

Node.js examples for Math:Math Function

Description

Round implementation

Demo Code


/*/*from   w w  w.j av  a2  s  .  c  om*/
  Lifted from https://github.com/mbostock/d3/wiki/Formatting#wiki-d3_round
  Returns the value rounded to n digits after the decimal point.

  @param {this} The number to round.
  @param {Number} n The number of digits after the decimal point. If omitted, defaults to 0.
  @return {Number} The modified number.
*/

// round implementation 

Number.prototype.round = function(n) {

  return n ? Math.round(this * (n = Math.pow(10, n))) / n : Math.round(this);

};

Related Tutorials