Javascript Number toString() method

Description

Javascript Number toString() method


Number.prototype.toString = function ()
{
    return this + '';
}

Javascript Number toString()

x=3.14159;//w  w w  .java2  s. co  m

Number.prototype.toString = function() {
  return this.toFixed(3);
}

console.log("x=%d", x);

Javascript Number toString()

/*//from w  w  w  . j  a  va  2 s. com
 * number.js
 *
 * This optional file simply modifies JS's Number.prototype.toString to better
 * print Infinity like Scheme's +inf.0
 */
Number.prototype.toString = function() {
    return function() {
        if(this == Infinity)
            return "+inf.0"
        else if(this == -Infinity)
            return "-inf.0"
        else
            // use magic native type coercion
            return ""+this
    }
}();



PreviousNext

Related