Affan Pathan
Affan Pathan

Reputation: 308

If condition for comparing integer and float does not working

I am using one If condition in javascript ,

var iid = "c_poqty_"+itemid;
var calculatedQuantity = document.getElementById(iid).value;

if(! isNaN(actualQuantity)) {
    if(actualQuantity >= calculatedQuantity) {
        return true;
    } else {
        alert("You must enter the order qty same or greater than the calculated PO Qty");
        document.getElementById(iid).focus();
        return false;
    }
} else {
    alert("Please Enter valid number");
    document.getElementById(iid).focus();
    return false;
}

Here, calculatedQuantity is always in float and while actualQuantity can be integer, I have one testcase:

calculatedQuantity = 1.0
actualQuantity = 1

Appreciate for your help!

Upvotes: 4

Views: 12641

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075059

Actually, I suspect they're both strings. Certainly calculatedQty is, as you've retrieved it from the value of an input field, and the value property's value is always a string. Use parseInt and/or parseFloat so you're comparing numbers rather than strings.

Consider:

console.log("1.0" > "1"); // "true"
console.log(1.0 > 1);     // "false"

Upvotes: 3

Related Questions