I am using a simple if/else to check if a value is greater than another value, and display an appropriate message.
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).
But it's not. For some reason, it's saying that totaltarget(300) is greater than totalbudget(1600) - and I cannot figure out why.
My PHP script, getHeaderBudgetvsTarget.php, is not doing anything special. 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.
The average
column in the main table is set to a 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?
Refer to the docs :
$.isNumeric()
returnstrue
only if the argument is of typenumber
, or if it's of typestring
and it can be coerced into finite numbers
So $.isNumeric("300")
is still true. You should convert your variables into number before comparing them like:
if (+totaltarget > +totalbudget)...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.