From the Floating-Point Guide:

What can I do to avoid this problem?

That depends on what kind of calculations you’re doing.

  • If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.
  • If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
  • If you have no decimal datatype available, an alternative is to work with integers, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.

Note that the first point only applies if you really need specific precise decimal behaviour. Most people don't need that, they're just irritated that their programs don't work correctly with numbers like 1/10 without realizing that they wouldn't even blink at the same error if it occurred with 1/3.

If the first point really applies to you, use BigDecimal for JavaScript or DecimalJS, which actually solves the problem rather than providing an imperfect workaround.

Answer from Michael Borgwardt on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toPrecision
Number.prototype.toPrecision() - JavaScript | MDN
The toPrecision() method of Number values returns a string representing this number to the specified number of significant digits. function precise(x) { return x.toPrecision(4); } console.log(precise(123.456)); // Expected output: "123.5" console.log(precise(0.004)); // Expected output: "0.004000" ...
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
🌐
npm
npmjs.com › package › number-precision
number-precision - npm
Perform addition, subtraction, multiplication and division operations precisely using javascript. Latest version: 1.6.0, last published: 3 years ago. Start using number-precision in your project by running `npm i number-precision`. There are 316 other projects in the npm registry using number-precision.
      » npm install number-precision
    
Published   Oct 08, 2022
Version   1.6.0
Author   cam song
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Numbers
Regular numbers in JavaScript are stored in 64-bit format IEEE-754, also known as “double precision floating point numbers”. These are numbers that we’re using most of the time, and we’ll talk about them in this chapter.
🌐
AVIO Consulting
avioconsulting.com › home › overcoming javascript numeric precision issues
Overcoming Javascript Numeric Precision Issues
March 4, 2024 - Integer numbers between -(253 -1) and 253+1 are accurately represented. Beyond this threshold, not all integer numbers can be represented. You can think that in Javascript, integer numbers are accurate up to 15 digits.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › floating-point-number-precision-in-javascript
Floating point number precision in JavaScript - GeeksforGeeks
July 11, 2025 - 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: ...
🌐
W3Schools
w3schools.com › jsref › jsref_toprecision.asp
JavaScript toPrecision() Method
The toPrecision() method formats a number to a specified length. A decimal point and nulls are added (if needed), to create the specified length. ... If you want to use W3Schools services as an educational institution, team or enterprise, send ...
🌐
TechOnTheNet
techonthenet.com › js › number_toprecision.php
JavaScript: Number toPrecision() method
In JavaScript, toPrecision() is a Number method that is used to convert a number to a specified precision (rounding the result where necessary) and return its value as a string.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. This means it can represent fractional values, but there are some limits to the stored number's magnitude and precision.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Number › toPrecision
JavaScript Number toPrecision() - Set Number Precision | Vultr Docs
November 27, 2024 - The toPrecision() method in JavaScript is a built-in function for the Number object that formats a numeric value to a specified length. This method provides a precision of the numerical representation, which can be particularly useful when you ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-number-toprecision-method
JavaScript Number toPrecision() Method - GeeksforGeeks
May 22, 2023 - Example 4: Passing an argument that results in the addition of null in the output, If the length of precision passed to the toPrecision() method is greater than the original number then zeros are appended to the input number to meet the specified ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Numbers_and_strings
Numbers and strings - JavaScript | MDN
In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e., a number between ±2^−1022 and ±2^+1023, or about ±10^−308 to ±10^+308, with a numeric precision of 53 bits).
🌐
GitHub
github.com › MikeMcl › decimal.js
GitHub - MikeMcl/decimal.js: An arbitrary-precision Decimal type for JavaScript
// Precision loss from using numeric literals with more than 15 significant digits. new Decimal(1.0000000000000001) // '1' new Decimal(88259496234518.57) // '88259496234518.56' new Decimal(99999999999999999999) // '100000000000000000000' // Precision loss from using numeric literals outside the range of Number values. new Decimal(2e+308) // 'Infinity' new Decimal(1e-324) // '0' // Precision loss from the unexpected result of arithmetic with Number values. new Decimal(0.7 + 0.1) // '0.7999999999999999' As with JavaScript numbers, strings can contain underscores as separators to improve readability.
Starred by 7.1K users
Forked by 494 users
Languages   JavaScript
🌐
Mikemcl
mikemcl.github.io › decimal.js
decimal.js API
An arbitrary-precision Decimal type for JavaScript.
🌐
Stack Abuse
stackabuse.com › how-to-set-floating-point-precision-in-javascript
How to Set Floating-Point Precision in JavaScript
February 22, 2022 - If the floating-point number didn't offer precision up to the fixed point you're formatting to - 0s are added as padding to the end.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toFixed
Number.prototype.toFixed() - JavaScript | MDN
The toFixed() method of Number values returns a string representing this number using fixed-point notation with the specified number of decimal places.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript number toprecision
JavaScript Number toPrecision
September 1, 2008 - This method returns a formetted number to the specified precision. The JavaScript number toPrecision() method formats a number to a specified precision or length.
🌐
Math.js
mathjs.org › docs › datatypes › numbers.html
math.js | an extensive math library for JavaScript and Node.js
Math.js uses the built-in JavaScript Number type. A Number is a floating point number with a limited precision of 64 bits, about 16 digits. The largest integer number which can be represented by a JavaScript Number is +/- 9007199254740992 (+/- 2^53). Because of the limited precision of floating ...