JS: Number.prototype.toFixed

By Xah Lee. Date: . Last updated: .
number.toFixed(n)
  • Return a string representation of number, with n digits after decimal, padded by 0 if necessary.
  • The number may be rounded up.
// truncate number

console.log(3.12345.toFixed(0) === "3");
console.log(3.12345.toFixed(1) === "3.1");
console.log((3.1).toFixed(3) === "3.100");

// the arg to toFixed cannot be negative. Else, RangeError
// toFixed may round up
console.log((3.56).toFixed(0) === "4");
console.log((3.56).toFixed(1) === "3.6");