<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ZovoCalc</title>
    <description>The latest articles on DEV Community by ZovoCalc (@zovocalc).</description>
    <link>https://dev.to/zovocalc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4147254%2Fa5cd376c-643a-4749-b3e9-4635349dbcd3.png</url>
      <title>DEV Community: ZovoCalc</title>
      <link>https://dev.to/zovocalc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zovocalc"/>
    <language>en</language>
    <item>
      <title>Why 0.1 + 0.2 Breaks Your JavaScript Calculator (and 4 Ways to Fix It)</title>
      <dc:creator>ZovoCalc</dc:creator>
      <pubDate>Mon, 28 Sep 2026 13:52:37 +0000</pubDate>
      <link>https://dev.to/zovocalc/why-01-02-breaks-your-javascript-calculator-and-4-ways-to-fix-it-3nd4</link>
      <guid>https://dev.to/zovocalc/why-01-02-breaks-your-javascript-calculator-and-4-ways-to-fix-it-3nd4</guid>
      <description>&lt;p&gt;If you've ever built a calculator in JavaScript, you've probably typed &lt;code&gt;0.1 + 0.2&lt;/code&gt; and watched it print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;
&lt;span class="c1"&gt;// 0.30000000000000004&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your users will notice, and they'll assume your calculator is broken. Here's why it happens and four practical ways to handle it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;JavaScript numbers are IEEE 754 double-precision floats. They store values in binary, and many "simple" decimals, like 0.1, have no exact binary representation. It's the same reason 1/3 can't be written exactly in decimal: 0.3333… goes on forever.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;0.1&lt;/code&gt; is really stored as something like &lt;code&gt;0.1000000000000000055511151231257827&lt;/code&gt;, and the tiny errors add up when you do arithmetic.&lt;/p&gt;

&lt;p&gt;A few more examples that surprise people:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;          &lt;span class="c1"&gt;// 0.30000000000000004&lt;/span&gt;
&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;        &lt;span class="c1"&gt;// 1.2100000000000002&lt;/span&gt;
&lt;span class="mf"&gt;0.3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;        &lt;span class="c1"&gt;// 0.19999999999999998&lt;/span&gt;
&lt;span class="mi"&gt;9007199254740993&lt;/span&gt; &lt;span class="c1"&gt;// 9007199254740992 (past Number.MAX_SAFE_INTEGER)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fix 1: Round for display, not for calculation
&lt;/h2&gt;

&lt;p&gt;The simplest fix for most calculators: keep full precision internally, and round only when you show the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;formatResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// toPrecision trims floating-point noise; Number() drops trailing zeros&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toPrecision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;formatResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "0.3"&lt;/span&gt;
&lt;span class="nf"&gt;formatResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "1.21"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Twelve significant digits hides the noise while keeping more precision than most handheld calculators display.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 2: Compare with a tolerance
&lt;/h2&gt;

&lt;p&gt;Never compare floats with &lt;code&gt;===&lt;/code&gt;. Use a small tolerance instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nearlyEqual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;eps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;eps&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nf"&gt;nearlyEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matters for things like checking whether a result is an integer before showing it without decimals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 3: Work in integers for money
&lt;/h2&gt;

&lt;p&gt;For currency, convert to the smallest unit (cents or paise) and do integer math:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toCents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;19.99&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;toCents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.01&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2500&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                     &lt;span class="c1"&gt;// "25.00"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For anything serious, like tax or interest over many periods, use a decimal library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 4: Use a decimal or BigInt library when exactness matters
&lt;/h2&gt;

&lt;p&gt;If users need exact decimal arithmetic or very large integers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BigInt&lt;/strong&gt; (built in) handles arbitrarily large integers: &lt;code&gt;2n ** 100n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;decimal.js&lt;/strong&gt; or &lt;strong&gt;big.js&lt;/strong&gt; handle arbitrary-precision decimals&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fraction.js&lt;/strong&gt; keeps exact fractions like 1/3 without converting to decimals&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are slower than plain numbers, so use them only where exactness matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: trig functions and "almost zero"
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Math.sin(Math.PI)&lt;/code&gt; returns &lt;code&gt;1.2246467991473532e-16&lt;/code&gt;, not &lt;code&gt;0&lt;/code&gt;, because π itself can't be stored exactly. Significant-digit rounding alone won't fix this one, because &lt;code&gt;1.22e-16&lt;/code&gt; is a perfectly valid 12-digit number. Snap tiny results to zero before formatting: &lt;code&gt;if (Math.abs(x) &amp;lt; 1e-12) x = 0;&lt;/code&gt; It's also why a degree-mode calculator should convert degrees to radians with &lt;code&gt;deg * Math.PI / 180&lt;/code&gt; and then clean up the result for display.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Floating-point noise is expected. It isn't a bug in your code.&lt;/li&gt;
&lt;li&gt;Round for &lt;strong&gt;display&lt;/strong&gt;, compare with a &lt;strong&gt;tolerance&lt;/strong&gt;, and use &lt;strong&gt;integers or decimal libraries&lt;/strong&gt; where exactness matters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the kinds of details we deal with building &lt;a href="https://zovocalc.com/" rel="noopener noreferrer"&gt;ZovoCalc&lt;/a&gt;, a free browser-based scientific calculator. If you're building your own calculator or math tool, I hope this saves you a debugging session.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
