• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3883
  • Last Modified:

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
0
tia_kamakshi
Asked:
tia_kamakshi
1 Solution
 
BraveBrainCommented:
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');
}
0
 
msd_informatiqueCommented:
I think your problem is that you are comparing the numbers as string...
use:
var varPaymentRequired = parseFloat('77249.83');
var varPayingAmount = parseFloat('44444444444');

Should work...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now