Issue with Comparing Two Numbers in JavaScript

issue with comparing two numbers in javascript

Convert the values to numbers:

if(Number(l) > Number(h)){ /* … */ }

or alternatively, parse each string as a float:

if(Number.parseFloat(l) > Number.parseFloat(h)){ /* … */ }

or as an integer:

if(Number.parseInt(l, 10) > Number.parseInt(h, 10)){ /* … */ }

How do i compare 2 numbers in JavaScript

The problem comes from your Tofixed function which converts the decimal obtained with parseFloat into a string.
So you have to surround it with Number ().

var textbox = document.getElementById('textbox');var entertedAmount = Number(parseFloat(textbox.value).toFixed(2));var maxPrice = Number(parseFloat(5000).toFixed(2));

if (entertedAmount <= maxPrice) { console.log(entertedAmount + " less than "+ maxPrice ); } else { alert('Please enter a price less than R'); textbox.value = 0; }
<input id="textbox" type="number" value="522.5886"/>

Comparing equality of two numbers using JavaScript Number() function

new Number() will return object not Number and you can not compare objects like this. alert({}==={}); will return false too.

Remove new as you do not need to create new instance of Number to compare values.

Try this:

var fn = 20;var sn = 20;
alert(Number(fn) === Number(sn));

Comparing two numbers in JavaScript, what about the fact those are doubles and the use of == or ===?

As you found, == and === behave identically if both types are numbers.

When comparing numbers for equality, JavaScript obeys the IEEE 754 standard. For most purposes, this is no different than C's double type. It means JavaScript will not do any precision checks for you. When needed, you write your own code to handle precision loss.


That answers your question, but there is one very common misconception you brought up!

JavaScript numbers are double's and as such the == and === should never be used as is.

The actual math behind this is a bit more complicated. Once grasped, you get some leeway with precision checks.

Consider the following which are guaranteed truths:

5 + 6 === 11
3 + 0.5 === 3.5
1.1 + 1 === 2.1
1.230000001 === 1.230000001
40 / 2 === 20
-0 === +0

But still, the following could be true or false depending on precision:

1.23 === 1.230000001
1.1 + 0.1 === 1.2
11 / 10 * 11 === 11 * 11 / 10

To make dealing with doubles easier, these conditions will not cause a loss of precision for a large range of floating-point numbers:

  • Adding, subtracting and multiplying doubles without a fractional part
  • Dividing doubles without a fractional part and that are divisible
  • Adding together doubles which are represented as a fraction of denominator 2, 4, 8, 16, ...
  • Multiplying any double by 0, 1, -1, 2, -2, 4, -4, 8, -8, ...
  • Dividing any double by 1, -1, 2, -2, 4, -4, 8, -8, ...

Finally, if you want to know more about floating-point math, this article is heavily referenced (but may go into too much detail for your needs): What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Javascript comparing number giving wrong answer

i think you're comparing 2 string. So, result is incorrect.

"10" > "2" ==> result is false

I suggest to compare like this:

+"10" > +'2" ==> result will be true ( in this case: +elC4.value > +result )

Hope this can help you, bro!

Comparing two numbers from a form in JavaScript

The values a and b are both strings. If you want to compare them like numbers, use parseInt() or parseFloat() on them before you perform the comparison.

So, before the if statement:

a = parseInt(a);
b = parseInt(b);

Comparing two numbers only compares the first number given jquery

.val() will return a string. You need to convert the values to numbers first before comparing:

if( Number($(this).val()) <= Number($('#anual_rate'+(number-1)).val())){

Javascript number comparison fails

Because you're comparing strings, not integers. Use parseInt to explicitly cast your values as integers.

Here's essentially what you're doing: jsFiddle example

To do the conversion, change your variables to something like:

var new_da1 = parseInt( document.getElementById('new_da1').value, 10);

(assuming they're all integers)

More efficient way of comparing two numbers

Technically the fastest way is by using bitwise operators. This method is ~35% faster in JavaScript than using ==.

!(a^b) or (a^b) == 0

Example:

!(1^1) // true
!(2^1) // false

Note: This doesn’t work for floating-point numbers and for values outside the range of 32-bit ints. ( thanks to Xufox for pointing this out )

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators



Related Topics



Leave a reply



Submit