最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If condition for comparing integer and float does not working - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question edited Nov 29, 2012 at 17:03 husayt 15.2k8 gold badges59 silver badges84 bronze badges asked Oct 31, 2012 at 8:55 Affan PathanAffan Pathan 3081 gold badge6 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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 paring numbers rather than strings.

Consider:

console.log("1.0" > "1"); // "true"
console.log(1.0 > 1);     // "false"
发布评论

评论列表(0)

  1. 暂无评论