If the condition for comparing integers and float does not work

I use 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 always in the float and while actualQuantity can be an integer, I have one test file:

 calculatedQuantity = 1.0 actualQuantity = 1 

Appreciate your help!

+4
source share
1 answer

Actually, I suspect they are both strings. Of course, there is calculatedQty , since you extracted it from the value the input field, and the value of the value property is always a string. Use parseInt and / or parseFloat to compare numbers, not strings.

Consider:

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

Source: https://habr.com/ru/post/1443179/


All Articles