offline version v3


35 of 264 menu

toFixed method

The toFixed method rounds a number to the specified decimal place (a position of a digit after a decimal point). To do this, you need to specify the number of decimal places in the parameter. If this number is not specified, then the default is 0 decimal places, that is, rounding to an integer.

Syntax

number.toFixed([number of decimal places]);

Example

Let there be a fraction. Let's round it to 3 decimal places:

let num = 1.1111; console.log(num.toFixed(3));

The code execution result:

1.111

Example

In the following example, there are also only 3 decimal places left, but the last digit increased by 1 because the rounding is done according to the rules of mathematics:

let num = 1.1119; console.log(num.toFixed(3));

The code execution result:

1.112

Example

In the following example, the fraction will be rounded to an integer because the method parameter is empty:

let num = 1.111; console.log(num.toFixed());

The code execution result:

1

Example

In the following example, the method parameter exceeds the number of decimal places in the original fraction, so the method will add two digits 0 to the end of our fraction:

let num = 1.1111; console.log(num.toFixed(6));

The code execution result:

1.111100

See also

enru