1

I have below jquery if\else comparison to evaluate part of code based on results,

if($('#order_quantity').val() < $('#available_quantity').val()) {
   // ---code to run----
} else {
   // --- code to show error---
}

This code work 3-4 times correctly for inputs and after that it working wrong,

when $('#order_quantity').val() = 5 and $('#available_quantity').val() = 46, then ideally --code to run-- should execute but it is running --code to show error -- of else statement.

Sometimes it correct and sometimes it not, I created console.log for above input values, inputs are correct but still comparison results throw wrong results

What can be problem in this comparison?

1
  • I think you want to parseInt or use Math.Round to get the desired value.. Commented Oct 4, 2015 at 7:38

5 Answers 5

6

The values read from the DOM are strings. You need to convert it to numbers in order to use it in arithmetic operation unless the operator coerces the operand to number.

if(+$('#order_quantity').val() < +$('#available_quantity').val())

You can convert string to number using

  1. By preceding the string with unary + operator, this will coerce the string to Number.
  2. By using Number function
  3. Using parseFloat
  4. Using parseInt, but this will convert the float value to integer.

this code work 3-4 times correctly for inputs and after that it working wrong

The code works for some values because how the strings are compared.

> "5" < "46" // false

The strings are compared character by character just like how sorting works. In this code the comparison is done as '5' < '4', when comparing the strings their ASCII values is compared. As the ASCII value of 5 is greater than that of 4 the comparison returns false.

Sign up to request clarification or add additional context in comments.

Comments

5

val() returns a string. You have to convert these string values in numbers:

if(Number($('#order_quantity').val()) < Number($('#available_quantity').val())) {
     // do something
} else {
     // do something
}

An example when your expression fails:

"24" < "124"
false

There are other ways to convert strings into numbers (see What's the fastest way to convert String to Number in JavaScript?):

  • Number(x)
  • parseInt(x, 10) (for integers)
  • parseFloat(x)
  • +x;
  • ~~x (for integers)
  • x | 0 (for integers)

...or using Math methods. x is the string value.

3 Comments

yeah, "1" < "2" returns true. But, "2" < "112" returns false
@SheraliTurdiyev That's because that is the behaviour of strings. It is compared using the character by character basis. So in the second example is translated as '2' < '1' and as the ASCII value of 2 is greater than that of 1, the condition returns false. Hope that makes sense. This problem is also observed when sorting the numbers, there also you've to explicitly convert the string to number before comparing
@SheraliTurdiyev Yes, for the same reason why "a" < "b" is true.
2

You will need to convert string to integer as all values read with jQuery val() method are returned as strings.

You can also use this code -

if(parseInt($('#order_quantity').val()) < parseInt($('#available_quantity').val()))

Comments

2

Try these solution :

var order_quantity =  $('#order_quantity').val();
var available_quantity = $('#available_quantity').val();

if( Number( order_quantity ) < Number( available_quantity ) )
{ //---code to run---- }
else
{ //--- code to show error---}

Number

Or

if (order_quantity - available_quantity  < 0)
{ //---code to run---- }
else
{ //--- code to show error---}

This forces javascript to process a numeric calculation. Strings will be converted into numbers.

Comments

2

The type of $('#order_quantity').val() is string. '5' > '46' is true just like 'b' > 'a' is true. To convert string value to int value, you can use parseInt function. To get the intended result your code should be something like:

if (parseInt($('#order_quantity').val()) < parseInt($('#available_quantity').val()))
  {
    //---code to run----
  }
else
  {
    //--- code to show error---
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.