Javascript 中的简单减法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5153061/
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-10-25 16:10:36  来源:igfitidea点击:

Simple subtraction bug in Javascript

javascript

提问by Umair Jabbar

I was shocked to see such a basic subtraction bug in JS, I am sure most of you here would have experienced this issue, please help me with a work around here, All I am doing is subtracting a number from 100 which gives unexpected results, an example is stated below

我很震惊在 JS 中看到这样一个基本的减法错误,我相信这里的大多数人都会遇到这个问题,请帮我解决这里的问题,我所做的只是从 100 中减去一个数字,这会产生意想不到的结果,下面是一个例子

100 - 99.1
// returns 0.9000000000000057

Am I doing something wrong here ? :S I am confused

我在这里做错了吗?:SI 很困惑

采纳答案by Sachin Shanbhag

Floating point values are never accurate as you expect. You can use Number object to convert this to answer as you need.

浮点值永远不会像您期望的那样准确。您可以根据需要使用 Number 对象将其转换为答案。

Number(100).toFixed(2) - Number(99.1).toFixed(2)

回答by jrharshath

You are working with floating point numbers, not integers. This is expected.

您正在使用浮点数,而不是整数。这是意料之中的。

The reason is that you can't accurately represent numbers like 0.1 and 0.3 in binary. Just like you can't represent 1/3 accruately in decimal form.

原因是你不能用二进制准确地表示像 0.1 和 0.3 这样的数字。就像您不能以十进制形式准确表示 1/3 一样。

回答by Jacob Relkin

In JavaScript, all of the numeric types are Numberobjects, which are represented as double-precision floating point numbers.

在 JavaScript 中,所有数字类型都是Number对象,表示为双精度浮点数。

On another note, you can retrieve a string representation of these numbers to a specific decimal point by using Number's toFixedmethod, which accepts a number denoting how many decimal points you would like the returned string to represent based on the receiver.

另一方面,您可以使用Number'stoFixed方法将这些数字的字符串表示检索到特定的小数点,该方法接受一个数字,该数字表示您希望返回的字符串根据接收者表示的小数点数。

An example:

一个例子:

var five = 5.00001;
console.log(five.toFixed(1)); //5.0

回答by Tollef Fog Heen

This is how floating point numbers work, they are not exact. http://en.wikipedia.org/wiki/Floating_pointmight be useful reading.

这就是浮点数的工作原理,它们并不准确。 http://en.wikipedia.org/wiki/Floating_point可能是有用的阅读。

回答by M K Garwa

At first prevent yourself from assigning the float value to variable then do it with direct expression to a variable. here is an example:

首先防止自己将浮点值分配给变量,然后使用直接表达式来执行变量。这是一个例子:

**var c = 80.35-20.35;
alert(c.toFixed(2));**

this will assign a 60.00 but other way to solve this will return 59.99999999.

这将分配 60.00,但其他解决此问题的方法将返回 59.99999999。

回答by Viranga

This is not a perfect answer but it works.

这不是一个完美的答案,但它有效。

function format_float_bug(num)
{
   return parseFloat( num.toFixed(15) ); 
} 

you can use as follows,

你可以使用如下,

format_float_bug(4990 % 10);

because below number (49.89999999999999857891452848) first 15 decimal places are like 9999999

因为下面的数字 (49.8999999999999857891452848) 前 15 个小数位就像 9999999