javascript浮点型的一些坑

      发布在:前端技术      评论:0 条评论
<h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">前言</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">javascript浮点型运算有很多问题,不管是加减还是乘除,还是四舍五入,都可能出现问题。本文简单介绍一些解决办法,都是工作中总结出来的,有简单,有麻烦。下面就总结一下。</p><h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">加法运算</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">例如常见的:</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">0.1</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="lit" style="color: rgb(0, 102, 102);">0.2</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//0.30000000000000004</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="lit" style="color: rgb(0, 102, 102);">1.4</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="lit" style="color: rgb(0, 102, 102);">2.8</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//4.199999999999999</span></code></pre><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">等等,这样的好多类似的运算</p><h3 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 17px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">解决方案一:</h3><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">简单方法:</p><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">这个方法要你知道保留小数点位数,好多情况不是很适用,特殊四舍五入的情况也不准确! 假如保留1位小数</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="pun" style="color: rgb(102, 102, 0);">(((</span><span class="lit" style="color: rgb(0, 102, 102);">0.1</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="lit" style="color: rgb(0, 102, 102);">0.2</span><span class="pun" style="color: rgb(102, 102, 0);">)*</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">toFixed</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">))/</span><span class="lit" style="color: rgb(0, 102, 102);">10</span></code></pre><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">保留2位小数</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="pun" style="color: rgb(102, 102, 0);">(((</span><span class="lit" style="color: rgb(0, 102, 102);">0.1</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="lit" style="color: rgb(0, 102, 102);">0.2</span><span class="pun" style="color: rgb(102, 102, 0);">)*</span><span class="lit" style="color: rgb(0, 102, 102);">100</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">toFixed</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">))/</span><span class="lit" style="color: rgb(0, 102, 102);">100</span></code></pre><h3 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 17px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">解决方案二</h3><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">精确方法: 定义函数</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);"> accAdd</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">var</span><span class="pln" style="color: rgb(0, 0, 0);"> r1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">pow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">max</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">)/</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">accAdd</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0.1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="lit" style="color: rgb(0, 102, 102);">0.2</span><span class="pun" style="color: rgb(102, 102, 0);">)</span></code></pre><h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">减法运算</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">例如:</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">0.3</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="lit" style="color: rgb(0, 102, 102);">0.1</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//0.19999999999999998</span></code></pre><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">封装函数方法</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);"> accSub</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">var</span><span class="pln" style="color: rgb(0, 0, 0);"> r1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;m</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">pow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">max</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="com" style="color: rgb(136, 0, 0);">//last modify by deeka</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="com" style="color: rgb(136, 0, 0);">//动态控制精度长度</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;n</span><span class="pun" style="color: rgb(102, 102, 0);">=(</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">&gt;=</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">)?</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">)/</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">toFixed</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></code></pre><h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">乘法运算</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">案例</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">0.69</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//6.8999999999999995</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="lit" style="color: rgb(0, 102, 102);">11</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">22.9</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//251.89999999999998</span></code></pre><h3 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 17px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">解决方案一</h3><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">如果在知道小数位个数的前提下,可以考虑通过将浮点数放大倍数到整型(最后再除以相应倍数),再进行运算操作,这样就能得到正确的结果了。 例如</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">0.69</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="lit" style="color: rgb(0, 102, 102);">11</span><span class="pun" style="color: rgb(102, 102, 0);">*(</span><span class="lit" style="color: rgb(0, 102, 102);">22.9</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">)/</span><span class="lit" style="color: rgb(0, 102, 102);">10</span></code></pre><h3 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 17px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">解决方案二</h3><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">封装函数方法</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);"> accMul</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">var</span><span class="pln" style="color: rgb(0, 0, 0);"> m</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">s1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">(),</span><span class="pln" style="color: rgb(0, 0, 0);">s2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);">s1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){}</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);">s2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){}</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Number</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">s1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">replace</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">&quot;&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">))*</span><span class="typ" style="color: rgb(102, 0, 102);">Number</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">s2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">replace</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">&quot;&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">))/</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">pow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">m</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">accMul</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0.69</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//6.9</span></code></pre><h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">除法运算</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">例如:</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">6.9</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//0.6900000000000001</span></code></pre><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);"><strong>封装函数方法</strong></p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);"> acc</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">var</span><span class="pln" style="color: rgb(0, 0, 0);"> t1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">t2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">t1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){}</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">t2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">)[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">].</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(102, 102, 0);">){}</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">with</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">){</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="typ" style="color: rgb(102, 0, 102);">Number</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">replace</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">&quot;&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="typ" style="color: rgb(102, 0, 102);">Number</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arg2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">replace</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&quot;.&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">&quot;&quot;</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">r1</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);">r2</span><span class="pun" style="color: rgb(102, 102, 0);">)*</span><span class="pln" style="color: rgb(0, 0, 0);">pow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">t2</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">t1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></code></pre><h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">四舍五入方法</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">案例如下:</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">10.145</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toFixed</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">---&gt;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&quot;10.14&quot;</span></code></pre><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">四舍五入之后应该是10.15,最后却变成了10.14</p><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">原因是因为</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="lit" style="color: rgb(0, 102, 102);">10.145</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toPrecision</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">21</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">---&gt;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&quot;10.1449999999999995737&quot;</span></code></pre><h3 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 17px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">解决方案一:</h3><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">简单解决方案还是先乘以100,然后,四舍五入。最后再除以100</p><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">例如</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">round</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10.145</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">100</span><span class="pun" style="color: rgb(102, 102, 0);">)/</span><span class="lit" style="color: rgb(0, 102, 102);">100</span></code></pre><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">当然,也可以用函数封装方法,我们将tofixed重新封装一下:</p><pre class="prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 18px; padding: 10px 10px 7px; color: rgb(51, 51, 51); font-size: 12px; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace; background-color: rgb(240, 240, 240); white-space: pre-wrap; line-height: 1.5em; border-width: 1px 1px 1px 2px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(27, 88, 184); border-image: initial;"><code style="margin: 0px; padding: 0px; background-color: transparent; font-family: Menlo, Monaco, Consolas, &quot;Lucida Console&quot;, &quot;Courier New&quot;, &quot;Microsoft Yahei&quot;, monospace;"><span class="typ" style="color: rgb(102, 0, 102);">Number</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">prototype</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toFixed </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n </span><span class="pun" style="color: rgb(102, 102, 0);">&gt;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">20</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">||</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(102, 102, 0);">&lt;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">throw</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">RangeError</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&#39;toFixed() digits argument must be between 0 and 20&#39;</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">const</span><span class="pln" style="color: rgb(0, 0, 0);"> number </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">isNaN</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">number</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">||</span><span class="pln" style="color: rgb(0, 0, 0);"> number </span><span class="pun" style="color: rgb(102, 102, 0);">&gt;=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">pow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">21</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> number</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">typeof</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&#39;undefined&#39;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">||</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(102, 102, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">round</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">number</span><span class="pun" style="color: rgb(102, 102, 0);">)).</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">let</span><span class="pln" style="color: rgb(0, 0, 0);"> result </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> number</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toString</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">const</span><span class="pln" style="color: rgb(0, 0, 0);"> arr </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">split</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">&#39;.&#39;</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="com" style="color: rgb(136, 0, 0);">// 整数的情况</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">arr</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">length </span><span class="pun" style="color: rgb(102, 102, 0);">&lt;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;result </span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&#39;.&#39;</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">let</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(102, 102, 0);">&lt;</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result </span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&#39;0&#39;</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">const</span><span class="pln" style="color: rgb(0, 0, 0);"> integer </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> arr</span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">];</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">const</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">decimal</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> arr</span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">];</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">decimal</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">length </span><span class="pun" style="color: rgb(102, 102, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">decimal</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">length </span><span class="pun" style="color: rgb(102, 102, 0);">&lt;</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">let</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(102, 102, 0);">&lt;</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">decimal</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">length</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result </span><span class="pun" style="color: rgb(102, 102, 0);">+=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&#39;0&#39;</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;result </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> integer </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">&#39;.&#39;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">decimal</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">substr</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">const</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">last</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">decimal</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">substr</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="com" style="color: rgb(136, 0, 0);">// 四舍五入,转换为整数再处理,避免浮点数精度的损失</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">parseInt</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">last</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">&gt;=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">const</span><span class="pln" style="color: rgb(0, 0, 0);"> x </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">pow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;result </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Math</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">round</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="pln" style="color: rgb(0, 0, 0);">parseFloat</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">result</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln" style="color: rgb(0, 0, 0);"> x</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);"> x</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp; &nbsp; &nbsp;result </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">toFixed</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> &nbsp; &nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">};</span></code></pre><h2 style="margin: 0px 0px 10px; padding: 4px 0px; font-size: 21px; border-bottom: 1px dashed rgb(204, 204, 204); color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">小结</h2><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">处理此类问题,其实都有简单办法,不过,有时候在某种场景下面,简单办法不一定适用罢了。 简单办法一般是:&nbsp;<strong>将浮点数乘以(扩大)10的n次方倍,把浮点数变为整数后再进行相应的运算,最后将得到的结果除以(缩小)10的n次方倍</strong>。 但是有时候我们并不知道浮点数的位数,及要精确的小数位数,因此,面对这种场景,我们就要用函数了!</p><p style="margin-top: 0px; margin-bottom: 1em; padding: 0px; color: rgb(68, 68, 68); font-family: &quot;Microsoft Yahei&quot;, &quot;Trebuchet MS&quot;, Georgia, &quot;Times New Roman&quot;, Times, sans-serif; white-space: normal; background-color: rgb(252, 252, 250);">来源:<a href="https://www.haorooms.com/post/js_float_bug">https://www.haorooms.com/post/js_float_bug</a></p><p><br/></p>
相关文章
热门推荐