Number.prototype.toFixed
Formats a number using fixed-point notation with the specified number of decimal places
Syntax
number.toFixed(digits?)Parameters
| Parameter | Type | Description |
|---|---|---|
| digits | number | Number of digits after the decimal point (0-100). Defaults to 0 |
Return Value
A string representing the number in fixed-point notation
Examples
const num = 3.14159;
console.log(num.toFixed(2)); // '3.14'
console.log(num.toFixed(4)); // '3.1416'const price = 9.9;
console.log('$' + price.toFixed(2)); // '$9.90'const value = 1.005;
console.log(value.toFixed(2)); // '1.00' (rounding edge case)
console.log(Math.round(value * 100) / 100); // 1.01Understanding Number.prototype.toFixed
The Number.prototype.toFixed method in JavaScript formats a number using fixed-point notation with the specified number of decimal places. It belongs to the Number object and is one of the most widely used methods for working with number values in modern JavaScript and TypeScript applications.
The method signature is number.toFixed(digits?). It accepts 1 parameter: digits. When called, it returns a string representing the number in fixed-point notation. Understanding when and how to use toFixed() helps you write more expressive, readable code.
Common use cases for Number.prototype.toFixed include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like number-toprecision, number-toexponential, number-tostring, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Number.prototype.toFixed is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Number.prototype.toPrecisionReturns a string representing the Number object to the specified precision
Number.prototype.toExponentialReturns a string representing the Number object in exponential notation
Number.prototype.toStringReturns a string representing the specified Number object in the specified radix (base)
More Number Methods
Other methods in the Number object
Related Tools
More Number Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.