Javascript Number toFixedDown(digits)

Description

Javascript Number toFixedDown(digits)


Number.prototype.toFixedDown = function(digits) {
    var n = this - Math.pow(10, -digits)/2;
    n += n / Math.pow(2, 53); // added 1360765523: 17.56.toFixedDown(2) === "17.56"
    return n.toFixed(digits);
}

Javascript Number toFixedDown(digits)

Number.prototype.toFixedDown = function(digits) {
 var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"),
  m = this.toString().match(re);//from   www.j av a2 s  .  c  om
  return m ? parseFloat(m[1]) : this.valueOf();
};



PreviousNext

Related