• JavaScript Video Tutorials

JavaScript Number toFixed() Method



The JavaScript Number toFixed() method returns the representation of a numerical value with or without decimal places. It includes an optional parameter called 'digits'. However, if the digits parameter is not within the range of '[0,100]', an exception of 'RangeError' will be thrown. In addition, if we try to invoke this method on an object that is not a number, a 'TypeError' exception will be thrown.

Note: If the number of decimal places is greater than the number of decimals in the original number, additional zeros will be added to the end of the number.

Syntax

Following is the syntax of JavaScript Number toFixed() method −

toFixed(digits)

Parameters

This method accepts an optional parameter called "digits", which is described below −

  • digits (optional) − The number of digits that appear after the decimal point.

Return value

This method returns the representation of numerical value with(or without) decimal value.

Example 1

The following example demonstrates the usage of the JavaScript Number toFixed() method.

<html>
<head>
<title>JavaScript toFixed() Method</title>
</head>
<body>
<script>
   const val = 123.44;
   document.write("Given value = ", val);
   document.write("<br>Result = ", val.toFixed());
</script>
</body>
</html>

Output

The above program produces the output shown below −

Given value = 123.44
Result = 123

Example 2

If we set the optional parameter 'digits' to 3, this method will represent a numerical value with 3 digits after the decimal point.

<html>
<head>
<title>JavaScript toFixed() Method</title>
</head>
<body>
<script>
   const val = 4553.4321343;
   const digits = 3;
   document.write("Given value = ", val);
   document.write("<br>Digits = ", digits);
   document.write("<br>Result = ", val.toFixed(digits));
</script>
</body>
</html>

Output

After executing the above program, it returns a number with the specified number of digits after the decimal point −

Given value = 4553.4321343
Digits = 3
Result = 4553.432

Example 3

The Number toFixed() method throws a "RangeError" exception if the value of the optional parameter 'digits' is outside the range of [0, 100].

<html>
<head>
<title>JavaScript toFixed() Method</title>
</head>
<body>
<script>
   const val = 123.321123;
   const digits = -1;
   document.write("Given value = ", val);
   document.write("<br>Digits = ", digits);
   try {
      document.write("<br>Result = ", val.toFixed(digits));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

Once the above program is executed, it throws a 'RangeError' exception −

Given value = 123.321123
Digits = -1
RangeError: toFixed() digits argument must be between 0 and 100

Example 4

If we invoke the Number toFixed() method on an object that is not a number, it throws a "TypeError" exception.

<html>
<head>
<title>JavaScript toFixed() Method</title>
</head>
<body>
<script>
   const val = "abc";
   const digits = 5;
   document.write("Given value = ", val);
   document.write("<br>Digits = ", digits);
   try {
      document.write("<br>Result = ", val.toFixed(digits));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

The above program throws a 'TypeError' exception as −

Given value = abc
Digits = 5
TypeError: val.toFixed is not a function
Advertisements