[英]comparing decimal numbers in javascript
im having issues comparing km distance, it saying in the example above, im comparing distance between locations, these locations are in km. 我在比较km距离时遇到问题,它在上面的示例中说,im在比较位置之间的距离时,这些位置以km为单位。
9,441.4<1500
The output is telling me that 9 thousand is less than 1 thousand. 输出告诉我9000少于1000。 In my case, should i only put or add in 1500 the dot and comma to be 1,500.0, or there is more than just that?
就我而言,我应该只将1500点或逗号加或加到1,500.0,还是不止于此?
You should remove the comma. 您应该删除逗号。 You don't use a thousands separator in JavaScript numbers.
您不要在JavaScript数字中使用千位分隔符。
So why didn't it fail with a syntax error? 那么,为什么它不会因语法错误而失败? Because it's valid syntax: JavaScript has a comma operator .
因为它是有效的语法:JavaScript具有逗号运算符 。 Your code was the number 9, followed by the comma operator, followed by the comparison
441.4 < 1500
. 您的代码是数字9,后跟逗号运算符,然后是比较
441.4 < 1500
。 The comma operator is one of JavaScript's more...interesting...operators: It evaluates the left-hand operand ( 9
in your example), throws away the resulting value, then evaluates its right-hand operand ( 441.4<1500
in your example) and takes the resulting value as the value of the comma operator expression. 逗号运算符是JavaScript中更...有趣的运算符之一:它计算左操作数(在您的示例中为
9
),丢弃结果值,然后计算其右操作数(在您的示例中为441.4<1500
例如),并将结果值用作逗号运算符表达式的值。
You wanted: 你自找的:
9441.4<1500
As TJ Crowder said, you need to remove the comma, and convert the string to a floating point number: 就像TJ Crowder所说的那样,您需要删除逗号,并将字符串转换为浮点数:
$api_input = "9,441.4";
$tmp = str_replace( ",", "", $api_input);
$distance = floatval($tmp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.