JavaScript Number toFixed() method - JavaScript Tutorial

JavaScript Number toFixed() method

BLUF: This tutorial on JavaScript Number toFixed() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Number toFixed() method

Understanding JavaScript Number toFixed() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The toFixed method in JavaScript is utilized to return a string that represents a number with a specified number of digits following the decimal point, rather than in exponential form.

Syntax

The syntax for the toFixed method is illustrated as follows:

Example

Number.toFixed(num)

Parameter

num - This parameter indicates the quantity of digits that should be displayed following the decimal point.

Return

A string that denotes a numerical value with a precise number of digits following the decimal point.

JavaScript Number toFixed method example

In this section, we will explore the toFixed method by examining several examples.

Example 1

Let's see a simple example of toFixed method.

Example

<script>

var a=98.9721;

document.writeln(a.toFixed());

</script>

Output:

Example 2

Let’s examine an example of how to round a number to a specified number of decimal places.

Example

<script>

var a=98.9721;

document.writeln(a.toFixed(2));

</script>

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience