javascript 比较javascript中的负数

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

Comparing negative numbers in javascript

javascriptcomparisonnumbers

提问by Tom G

I'm sure this is a simple problem, but I'm comparing negative numbers in javascript i.e.:

我确定这是一个简单的问题,但我正在比较 javascript 中的负数,即:

var num1 = -83.778;
var num2 = -83.356;

if(num1 < num2)
{
    // Take action 1
}
else
{
    // Take action 2
}

This script will always take action 2, even though num1is less than num2. Whats going on here?

此脚本将始终执行操作 2,即使num1小于num2. 这里发生了什么?

回答by Seth

How does if (parseFloat(num1) < parseFloat(num2))work? Maybe you're numbers are turning into strings somewhere.

如何if (parseFloat(num1) < parseFloat(num2))工作?也许你的数字在某个地方变成了字符串。