Javascript 为什么 2 个 parseFloat 变量的总和给我一个不正确的十进制数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3650081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:35:45  来源:igfitidea点击:

Why does the sum of 2 parseFloat variables give me an incorrect decimal number

javascriptfunctionsum

提问by Kennethvr

If I have this little function:

如果我有这个小功能:

<script type="text/javascript">

 function printFloat(){
      var myFloatNumber1 = document.getElementById('floatNumber1');
      var myFloatNumber2 = document.getElementById('floatNumber2');
      alert(parseFloat(myFloatNumber1.value) + parseFloat(myFloatNumber2.value))
 }

</script>

<input type="text" id="floatNumber1"></input>
<input type="text" id="floatNumber2"></input>

<input type="button" onclick="printFloat()"/>

in field 1 I enter: 221.58 in field 2 I enter: 2497.74

在字段 1 我输入:221.58 在字段 2 我输入:2497.74

I expect the sum of 2 numbers in the input fields to be a 2 number digit: 2719.32 But the result is a incorrect number... : 2719.3199999999997

我希望输入字段中 2 个数字的总和为 2 个数字:2719.32 但结果是一个不正确的数字...:2719.3199999999997

a round would do the job, but I just don't get why the code does that on this number... On other number combinations, the sum is correct...

一轮可以完成这项工作,但我只是不明白为什么代码会在这个数字上这样做......在其他数字组合上,总和是正确的......

回答by Michael Borgwardt

From The Floating-Point-Guide:

浮点指南:

Why don't my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

为什么我的数字,比如 0.1 + 0.2 加起来是一个不错的回合 0.3,而是我得到一个奇怪的结果,比如 0.30000000000000004?

因为在内部,计算机使用的格式(二进制浮点数)根本无法准确表示像 0.1、0.2 或 0.3 这样的数字。

在编译或解释代码时,您的“0.1”已经四舍五入到该格式中最接近的数字,这甚至在计算发生之前就会导致一个小的舍入误差。

In your case, the rounding errors happen when the values you entered are converted by parseFloat().

在您的情况下,当您输入的值由parseFloat().

Why do other calculations like 0.1 + 0.4 work correctly?

In that case, the result (0.5) can be represented exactly as a floating-point number, and it's possible for rounding errors in the input numbers to cancel each other out - But that can't necessarily be relied upon (e.g. when those two numbers were stored in differently sized floating point representations first, the rounding errors might not offset each other).

In other cases like 0.1 + 0.3, the result actually isn't really 0.4, but close enough that 0.4 is the shortest number that is closer to the result than to any other floating-point number. Many languages then display that number instead of converting the actual result back to the closest decimal fraction.

为什么像 0.1 + 0.4 这样的其他计算能正常工作?

在这种情况下,结果 (0.5) 可以精确地表示为一个浮点数,并且输入数字中的舍入错误可能会相互抵消 - 但这不一定是可靠的(例如,当这两个数字首先存储在不同大小的浮点表示中,舍入误差可能不会相互抵消)。

在其他情况下,如 0.1 + 0.3,结果实际上并不是真正的 0.4,但足够接近以至于 0.4 是最短的数字,它比任何其他浮点数更接近结果。许多语言然后显示该数字,而不是将实际结果转换回最接近的十进制分数。

回答by Maulik Kanani

simply do following.

简单地做以下。

function printFloat(){
      var myFloatNumber1 = document.getElementById('floatNumber1');
      var myFloatNumber2 = document.getElementById('floatNumber2');
      var total=parseFloat(myFloatNumber1.value) + parseFloat(myFloatNumber2.value);
      alert(total.toFixed(2));
 }