Returns the number with at most `places` digits after the dot - Node.js Number

Node.js examples for Number:Parse

Description

Returns the number with at most `places` digits after the dot

Demo Code


//returns the number with at most `places` digits after the dot
//examples:/*from  w  ww .ja  v a 2s. c o  m*/
// 1.337.maxDecimal(1) === 1.3
//
//steps:
// floor(1.337 * 10e0) = 13
// 13 / 10e0 = 1.3
Number.prototype.maxDecimal = function ( places ) {
  var exponent = Math.pow( 10, places );

  return Math.floor( this * exponent ) / exponent;
};

Related Tutorials