Warning: Floating point numbers cannot represent all decimals precisely in binary. This can lead to unexpected results, such as 0.1 + 0.2 === 0.3 returning false .
Number.prototype.toFixed(fractionDigits) NOTE 1 : toFixed returns a String containing this Number value represented in decimal fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits is undefined, 0 is assumed.
The following steps are performed:
1. Let x be thisNumberValue(this value). 2. ReturnIfAbrupt(x). 3. Let f be ToInteger(fractionDigits). (If fractionDigits is undefined, this step produces the value 0). 4. ReturnIfAbrupt(f). 5. If f < 0 or f > 20, throw a RangeError exception. However, an implementation is permitted to extend the behaviour of toFixed for values of f less than 0 or greater than 20. In this case toFixed would not necessarily throw RangeError for such values. 6. If x is NaN, return the String "NaN". 7. Let s be the empty String. 8. If x < 0, then Let s be "-". Let x = –x. 9. If x ≥ 10^21, then Let m = ToString(x). 10.Else x < 1021, a. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as possible. If there are two such n, pick the larger n. b. If n = 0, let m be the String "0". Otherwise, let m be the String consisting of the digits of the decimal representation of n (in order, with no leading zeroes). c. If f ≠ 0, then i. Let k be the number of elements in m. ii. If k ≤ f, then 1. Let z be the String consisting of f+1–k occurrences of the code unit 0x0030. 2. Let m be the concatenation of Strings z and m. 3. Let k = f + 1. iii. Let a be the first k–f elements of m, and let b be the remaining f elements of m. iv. Let m be the concatenation of the three Strings a, ".", and b. 11. Return the concatenation of the Strings s and m.