Floating Point Precision in JavaScript

If you have ever worked with floating-point numbers in JavaScript, then you might have found weird results sometimes, the addition of 0.1 and 0.2 would have given you 0.30000000000000004, not exactly 0.3. This unexpected behaviour happens because JavaScript uses the IEEE 754 standard for representing numbers, and this can create small precision errors in floating-point number calculations. In this blog, you will learn why these errors occur and how you can deal with them efficiently.

Table of Contents:

Common Issues With Floating-Point Number Precision

JavaScript stores numbers as 64-bit floating-point numbers or IEEE 754 format. Some decimal numbers like 0.1 and 0.2 cannot be represented accurately in binary. As a result, calculations of such floating-point numbers may produce small inaccuracies in results. Here are some common issues with floating-point number precision:

Kickstart your career in tech with hands-on web development training.
Start Your Web Development Journey Today!
quiz-icon

Issue 1: Unexpected Results

The addition of two floating-point numbers like 0.1 and 0.2 cannot give you 0.3. Instead of the value 0.3, it will give 0.30000000000000004.

Issue 2: Rounding Errors

Multiplication and division with floating-point numbers do not produce accurate results every time.

Example:

Javascript

Output:

Rounding Errors

Explanation: Another common issue of using floating-point numbers in the calculations is that whenever you’re using them in multiplications and divisions, they might produce incorrect results. For example, console.log(1.007 * 100) can print the result as 100.69999999999999, not 100.70.

How to Handle Floating-Point Precision in JavaScript

Here are a few methods to set the floating-point number precision in JavaScript:

Method 1: Using toFixed()

The toFixed() method is one of the simplest ways to set the precision of floating-point numbers in JavaScript. The toFixed(n) method rounds a number to n decimal places and returns the string.

Example:

Javascript

Output:

Using toFixed()

Explanation: In this example, the toFixed(1) operator is used to print a result with a precision of up to 1 decimal place. You can change this value as you want.

Method 2: Using toPrecision()

The toPrecision(n) method formats a number to n significant digits. It is useful for controlling the total number of digits rather than just decimals.

Example:

Javascript

Output:

Using toPrecision()

Explanation: In this example, the toPrecision() method is used to set the precision value for floating-point numbers. In this case, the .toPrecision(4) prints the value of length 4.

Method 3: Multiplication and Division Trick

A logical way to set the precision for floating-point numbers is to use multiplication and division both, when you are doing calculations using floating-point numbers.

Example:

Javascript

Output:

Multiplication and Division Trick

Explanation: In this example, Math.round((0.1 + 0.2) * 100) can calculate the result as 30, and when 30 is divided by 100, it prints the result as 0.3.

Method 4: Using an External Library

Sometimes, you require the result with strict precision. Then, the external libraries like Big.js, Decimal.js, and Math.js, which support high-precision calculations, are useful.

Example:

Javascript

Output:

Using an External Library

Explanation: Using an external library like Big.js is very helpful when you are dealing with high-precision calculations.

Best Practices

  • Always round numbers before using them in calculations.
  • Use toFixed() or toPrecision() if you want to print results only.
  • For financial calculations, consider using external libraries like Big.js or Decimal.js

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

Floating-point number precision issues are common in JavaScript due to its use of IEEE 754 floating-point arithmetic. However, by understanding the issues and the methods to solve this issue, like toFixed(), toPrecision(), and the Big.js library, you can efficiently manage floating-point number precision in your applications.

How to Set Floating Point Number Precision in JavaScript – FAQs

Q1. Why is 0.1 + 0.2 !== 0.3 in JavaScript?

JavaScript represents numbers using the IEEE 754 floating-point standard. Numbers like 0.1 and 0.2 cannot be represented in binary accurately, leading to small rounding errors.

Q2. How to compare floating-point numbers correctly?

For comparing floating-point numbers in JavaScript, use Number.EPSILON. It is best for checking the equality of floating-point numbers.

Q3. Which library is best for high-precision calculations?

For doing high-precision calculations, libraries like Big.js, Decimal.js, and Math.js are used. These libraries are useful to perform arithmetic operations with greater precision.

Q4. How do you limit float to 2 decimal places in JavaScript?

You can use toFixed(2) to limit floating numbers ar 2 decimal places in JavaScript.

Q5. What is the precision of a number in JavaScript?

JavaScript uses double-precision floating-point numbers (64-bit IEEE 754). The maximum safe integer is Number.MAX_SAFE_INTEGER (253 – 1), and the minimum difference between two numbers is Number.EPSILON (~2.22e-16).

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner