totn JavaScript

JavaScript: Number toFixed() method

This JavaScript tutorial explains how to use the Number method called toFixed() with syntax and examples.

Description

In JavaScript, toFixed() is a Number method that is used to convert a number to fixed-point notation (rounding the result where necessary) and return its value as a string. Because toFixed() is a method of the Number object, it must be invoked through a particular instance of the Number class.

Syntax

In JavaScript, the syntax for the toFixed() method is:

number.toFixed([decimalPlaces]);

Parameters or Arguments

decimalPlaces
Optional. It is the number of digits after the decimal place to display in the result. If this parameter is omitted, the decimalPlaces will default to 0.

Returns

The toFixed() method converts a number to fixed-point notation with the indicated number of decimalPlaces (rounding the result where necessary) and then returns its value as a string.

If more decimal places are required than was present in the original number, the toFixed() method will pad the result with 0's after the decimal place.

Note

  • The toFixed() method will round the resulting value if necessary.
  • The toFixed() method will pad the resulting value with 0's if there are not enough decimal places in the original number.
  • The toFixed() method does not change the value of the original number.

Example

Let's take a look at an example of how to use the toFixed() method in JavaScript.

For example:

var totn_number = 123.456789;

console.log(totn_number.toFixed());
console.log(totn_number.toFixed(1));
console.log(totn_number.toFixed(2));

In this example, we have invoked the toFixed() method using the Number class.

We have written the output of the toFixed() method to the web browser console log, for demonstration purposes, to show what the toFixed() method returns.

The following will be output to the web browser console log:

123
123.5
123.46

In this example, the first output to the console log returned the string value "123" which is the fixed-point notation for 123.456789 rounded to 0 decimal places.

The second output to the console log returned the string value "123.5" which is the fixed-point notation for 123.456789 rounded to 1 decimal place.

The third output to the console log returned the string value "123.46" which is the fixed-point notation for 123.456789 rounded to 2 decimal places.

Specifying an Exponential Notation

The toFixed() method can also handle converting exponential notation values to fixed-point notation.

So if we rewrote our numeric value of 123.456789 as the equivalent exponential value (123.456789 = 1.23456789e+2), we could change our example as follows:

var totn_number = 1.23456789e+2;

console.log(totn_number.toFixed());
console.log(totn_number.toFixed(1));
console.log(totn_number.toFixed(2));

The following will be output to the web browser console log:

123
123.5
123.46

In this example, the first output to the console log returned the string value "123" which is the fixed-point notation for 1.23456789e+2 rounded to 0 decimal places.

The second output to the console log returned the string value "123.5" which is the fixed-point notation for 1.23456789e+2 rounded to 1 decimal place.

The third output to the console log returned the string value "123.46" which is the fixed-point notation for 1.23456789e+2 rounded to 2 decimal places.

Padding the Decimal Places

Finally, let's explore how the toFixed() method pads the result with 0's if there are not enough decimal places in the original number.

For example:

var totn_number = 123.45;

console.log(totn_number.toFixed(3));
console.log(totn_number.toFixed(4));

The following will be output to the web browser console log:

123.450
123.4500

In this example, the first output to the console log returned the string value "123.450" which is the fixed-point notation for 123.45 padded to 3 decimal places with 0's.

The second output to the console log returned the string value "123.4500" which is the fixed-point notation for 123.45 padded to 4 decimal places with 0's.