Skip to content

How to Fix JavaScript Floating Point Precision Issues

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Fix JavaScript Floating Point Precision Issues. We cover key concepts, practical examples, and best practices.

The Problem

JavaScript floating point arithmetic produces unexpected results like 0.1 + 0.2 === 0.3 returning false due to IEEE 754 binary representation, where decimals like 0.1 cannot be represented exactly in binary.

Quick Fix

Step 1: Round to a fixed number of decimals

Use toFixed() or Math.round() for display:

const result = 0.1 + 0.2;
console.log(result);
0.30000000000000004

Round for comparison:

const result = 0.1 + 0.2;
const rounded = Math.round(result * 100) / 100;
console.log(rounded);
console.log(rounded === 0.3);
0.3
true

Step 2: Use Number.EPSILON for comparisons

Compare with a tolerance threshold:

function nearlyEqual(a, b) {
    return Math.abs(a - b) < Number.EPSILON;
}
console.log(nearlyEqual(0.1 + 0.2, 0.3));
true

Step 3: Convert to integers for monetary calculations

Multiply before calculating and divide after:

const price = 9.99;
const quantity = 3;
const total = price * quantity;
console.log(total);
29.969999999999999

Use integer cents:

const priceCents = 999;
const quantity = 3;
const totalCents = priceCents * quantity;
const total = (totalCents / 100).toFixed(2);
console.log(total);
29.97

Step 4: Use a decimal library

For exact decimal arithmetic, use a library:

// Using decimal.js
const Decimal = require('decimal.js');
const a = new Decimal('0.1');
const b = new Decimal('0.2');
const sum = a.plus(b).toNumber();
console.log(sum);
console.log(sum === 0.3);
0.3
true

Prevention

  • Never compare floating point numbers with === without rounding.
  • Use toFixed(2) for currency display.
  • Work in integer units (cents, pennies) for financial calculations.
  • Use Number.EPSILON for tolerance-based comparisons.
  • Use libraries like decimal.js or currency.js for exact arithmetic.

Common Mistakes with floating point

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world JS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does 0.1 + 0.2 not equal 0.3?

0.1 and 0.2 cannot be represented exactly in binary floating point (IEEE 754). They are approximated, and the addition of two approximations produces a result that is slightly off. This is not a JavaScript bug -- it affects every language that uses IEEE 754 floating point, including Python, Java, C, and Ruby.

How many decimal places are safe for floating point arithmetic?

JavaScript numbers are 64-bit doubles with about 15-17 significant digits. For values that stay within the safe integer range (up to 2^53), you can round to a reasonable number of decimal places. For financial calculations that require exactness, always use integer arithmetic or a decimal library.

Does BigInt solve floating point precision?

No. BigInt handles arbitrarily large integers but does not support decimal values at all. For exact decimal arithmetic, use libraries like decimal.js or the upcoming Decimal proposal for JavaScript (stage 3 as of 2024). BigInt is useful for large integer values but not for fractional calculations.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro