JavaScript: 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" ); // true

console.log(
 3.12345.toFixed(1) === "3.1" ); // true

console.log(
 (3.1).toFixed(3) === "3.100" ); // true

// the arg to toFixed cannot be negative. Else, RangeError

Rounded up example.

// toFixed may round up
console.log( (3.56).toFixed ( 0 ) === "4" ); // true
console.log( (3.56).toFixed ( 1 ) === "3.6" ); // true

[see Format Number]

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Number

prototype