Original array with rounded values - Node.js Array

Node.js examples for Array:Array Value

Description

Original array with rounded values

Demo Code

/**/*from w ww  .j  ava 2s. c o m*/
 *
 * @param {Number} decimalPlaces
 *      number of decimal places
 *
 * @return {Array} original array with rounded values
 *
 */
Array.prototype.round = function(decimalPlaces) {
    var multi = Math.pow(10,decimalPlaces);
    for (var i=0; i<this.length; i++) this[i] = Math.round(this[i] * multi)/multi;
    return this;
};

Related Tutorials