The Wayback Machine - https://web.archive.org/web/20240118173642/https://www.geeksforgeeks.org/floating-point-number-precision-in-javascript/
Open In App
Related Articles

Floating point number precision in JavaScript

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The representation of floating points in JavaScript follows the IEEE-754 format. It is a double-precision format where 64 bits are allocated for every floating point.

The displaying of these floating values could be handled using these methods: 

Using toFixed() Method

The number of decimal places in float values can be set using the toFixed() method. This method converts the number into a string, keeping the specified number of digits after the point. If no value is passed as a parameter, then it takes 0 as the default value i.e. no decimal points are displayed. 

Syntax:

number.toFixed(digits)

Example: This example shows the use of the toFixed() method.

Javascript




pi = 3.14159265359;
twoPlaces = pi.toFixed(2);
fivePlaces = pi.toFixed(5);
 
console.log(twoPlaces);
console.log(fivePlaces);


Output

3.14
3.14159

Using toPrecision() Method

The number of total digits in float values can be set using the toPrecision() method. This method converts the number into a string, keeping the total number of digits of the value as specified and rounding them to the nearest number. If no value is passed as a parameter, then the function acts as a toString() function effectively returning the value passed as a string. 

Syntax:

number.toPrecision(precision);

Example: This example shows the use of the toPrecision() method.

Javascript




pi = 3.14159265359;
twoPlaces = pi.toPrecision(2);
fivePlaces = pi.toPrecision(5);
 
console.log(twoPlaces);
console.log(fivePlaces);


Output

3.1
3.1416

Using toExponential() Method

JavaScript Number toExponential() method is used to convert a number to its exponential form. It returns a string representing the Number object in exponential notation. The toExponential() method is used with a number as shown in the above syntax using the ‘.’ operator. This method will convert a number to its exponential form.

Syntax:

number.toExponential(value)

Example: toExponential(2) represents the number 12345.6789 in exponential notation with 2 digits of precision. The output might look like 1.23e+4, where 1.23 is the significand and 4 is the exponent.

Javascript




const myNumber = 12345.6789;
 
// Using toExponential with precision
const exponentialNotation = myNumber.toExponential(2);
 
console.log(exponentialNotation);


Output

1.23e+4

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 16 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials