[英]greater than less than fail
I am using a simple if/else to check if a value is greater than another value, and display an appropriate message.我正在使用一个简单的 if/else 来检查一个值是否大于另一个值,并显示适当的消息。
Looks like this:看起来像这样:
$.post('api/getHeaderBudgetvsTarget.php', {headercriteria:headercriteria}, function(data) {
let obj = JSON.parse(data);
let totalbudget = obj[0].budget; // set to 1600
let totaltarget = obj[0].target; // set to 300
if(totaltarget > totalbudget) {
console.log('greater');
} else if(totaltarget < totalbudget) {
console.log('lesser');
}
});
Looking at the above logic, it should be clear to see that the console should display "lesser", because totaltarget(300) is not greater than totalbudget(1600).看上面的逻辑,应该清楚的看到控制台应该显示“lesser”,因为totaltarget(300)不大于totalbudget(1600)。
But it's not.但事实并非如此。 For some reason, it's saying that totaltarget(300) is greater than totalbudget(1600) - and I cannot figure out why.
出于某种原因,它说 totaltarget(300) 大于 totalbudget(1600) - 我不知道为什么。
My PHP script, getHeaderBudgetvsTarget.php, is not doing anything special.我的 PHP 脚本 getHeaderBudgetvsTarget.php 没有做任何特别的事情。 Just a simple query that does some calculations, as follows:
只是一个执行一些计算的简单查询,如下所示:
<?php
$sql = "SELECT
( SELECT ROUND(SUM(budget)) FROM `budget` ) AS 'budget',
( SELECT ROUND(SUM(`average`)) FROM `main` ) AS 'target',
( SELECT ROUND(SUM(`totalcheck`)) FROM `main` ) - ( SELECT ROUND( SUM(`budget`) ) FROM `budget` ) AS `budgetvstotalcheck`";
?>
Nothing special.没什么特别的。
I checked the database design.我检查了数据库设计。 The only difference I could find was the
budget
column in the budget table is set to a decimal.我能找到的唯一区别是预算表中的
budget
列设置为小数。
The average
column in the main table is set to a mediumint.主表中的
average
列设置为 mediumint。
I don't think either one of those settings really make a difference (let me know if otherwise).我认为这些设置中的任何一个都不会真正产生影响(如果不是,请告诉我)。
I am completely perplexed as to why this is happening.我完全不明白为什么会这样。
I did attempt the following:我确实尝试了以下操作:
if($.isNumeric(totalbudget)) {
console.log('true');
} else {
console.log('false');
}
I checked both variables, and both appear to be numeric.我检查了这两个变量,它们似乎都是数字。
Why is this particular greater/lesser than logic not working?为什么这个特定的大于/小于逻辑不起作用?
$.isNumeric()
returnstrue
only if the argument is of typenumber
, or if it's of typestring
and it can be coerced into finite numbers$.isNumeric()
仅当参数是number
类型,或者它是string
类型并且可以强制转换为有限数时才返回true
So $.isNumeric("300")
is still true.所以
$.isNumeric("300")
仍然是正确的。 You should convert your variables into number before comparing them like:在比较它们之前,您应该将变量转换为数字:
if (+totaltarget > +totalbudget)...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.