string Number.prototype.toFixed(fractionDigits:uint|mixed=0)
Returns a string with the number and a fixed number of digits after the decimal point.
Number.prototype.toFixed.length = 1
If toFixed is called with more than one argument the behaviour is undefined (see 15).
Number.prototype.toFixed = function(fractionDigits){
var f = ToInteger(fractionDigits);
if (f < 0 || f > 20) throw RangeError;
var x = this.[[PrimitiveValue]];
if (isNaN(x)) return "NaN";
var s = '';
if (x < 0) {
s = '-';
x = -x;
}
if (x >= Math.pow(10,21)) {
var m = ToString(x);
} else { // x < 10^21
var n; // closest number such that "n/Math.pow(10,f)" is 0. If two such n pick largest n.
if (n == 0) var m = "0";
else var m = n+'';
if (f != 0) {
var k = m.length;
if (k <= f) {
var z = new Array(f+2-k).join('0'); // +2 instead of 1 because of the join
var m = z + m;
k = f+1;
}
var a = m.substring(0, k-f);
var b = msubstring(k-f);
var m = a+"."+b;
}
}
return s+m;
}
Note that an implementation is allowed to accept a fracitonDigits argument larger than 20.
Note: toString only results in enough digits to distinguish the number from an adjacent number. There are cases where toFixed outputs more precise numbers than toString. Example:
(1000000000000000128).toString => "1000000000000000100"
(1000000000000000128).toFixed(0) => "1000000000000000128"