0.1 + 0.2 is not 0.3 in JavaScript.

This page summarizes the projects mentioned and recommended in the original post on /r/javascript

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • decimal.js

    An arbitrary-precision Decimal type for JavaScript

  • That's because every number in JavaScript is represented using 64 bits floating point. Since numbers are saved in binary, there are some decimal numbers that are periodics in binary but not in decimal, e.g. 0.2, equivalent to 0.00110011001100110011... in bin. You can avoid this using some BigDecimals libraries like decimal.js, or, if you need to work in different bases, you have Basenumber.js

  • Basenumber.js

    A BigDecimal library for arbitrary precision that allows you to work with numbers in different bases from 2 to 36.

  • That's because every number in JavaScript is represented using 64 bits floating point. Since numbers are saved in binary, there are some decimal numbers that are periodics in binary but not in decimal, e.g. 0.2, equivalent to 0.00110011001100110011... in bin. You can avoid this using some BigDecimals libraries like decimal.js, or, if you need to work in different bases, you have Basenumber.js

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • bignumber.js

    A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic

  • Obviously it's a disaster for financial calculations where accuracy is absolutely paramount, so for this you should be using integers/bigint and working in cents/pence, or use a package like BigNumber.js.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts