Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is the toFixed() method in JavaScript?

Umme Ammara

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The toFixed() method in JavaScript converts a number to a string, rounding it to a specified number of decimals. The functionality of the toFixed method is shown in the illustration below:

Syntax

The toFixed() method is declared as shown below:

Parameters

The toFixed() method takes an optional parameter. This parameter is an integer number that represents the number of digits after the decimal point. If the parameter is unspecified, the default value is zero.

If the number of digits in the parameter is greater than the actual number, the function adds zeroes to the number to create the specified decimal length.

Return value

The toFixed() method returns a string representing the number rounded to the decimal place specified in the parameter.

Examples

The following code shows some examples using the toFixed() method in JavaScript.

//Example 1
var num = 2.713678;
console.log(num.toFixed());
//Example 2
console.log(num.toFixed(2));
//Example 3
console.log(num.toFixed(10));

RELATED TAGS

javascript

CONTRIBUTOR

Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring