Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

compare double values in javascript

Hi,
I want to comapare 2 double values in javascript.

The problem is that if user types 44444444444 in the text box, then I think the value is converting in string and does not compare properly.

but If it is small number like 77249.83, then it works fine.

<html>
<head>

<script language="JavaScript">

      function validateTtlAmountPaid(theForm)
      {
            var varPaymentRequired = '77249.83';
            var varPayingAmount = '44444444444';


            if(varPaymentRequired == varPayingAmount)
            {
                  return true;
            }else if(varPayingAmount >= varPaymentRequired)
            {
                  alert("Amount is more than expected.");
                  return false;
            }else
            {
                  alert("here");
            }
      }

      </script>


</head>
<body onload="validateTtlAmountPaid()"></body>
</html>


I know that if we take variable like

var varPaymentRequired = 77249.83;
var varPayingAmount = 44444444444;

then it works fine, but the problem is that in text box if user type 44444444444' for variable varPayingAmount then this does not works fine.

In my actuall code, I have written the above 2 variables like this

var varPaymentRequired = document.Form1.<%=txtPaymentRequired.ClientID%>.value;
var varPayingAmount = document.Form1.<%=txtInvoicePayment.ClientID%>.value;

What is the prob?

Please help
Avatar of BraveBrain
BraveBrain
Flag of Norway image

Sounds strange enough...

Try this:
var varPaymentRequired = new Number(document.Form1.<%=txtPaymentRequired.ClientID%>.value);
var varPayingAmount = new Number(document.Form1.<%=txtInvoicePayment.ClientID%>.value);

And maybe also put in (for test purposes)
if (isNaN(varPayingAmount)) {
  alert('varPayingAmount is not a number');
}
ASKER CERTIFIED SOLUTION
Avatar of msd_informatique
msd_informatique

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial