tia_kamakshi
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(theF orm)
{
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="validateTtlAmountP aid()"></b ody>
</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.<%=txtPayme ntRequired .ClientID% >.value;
var varPayingAmount = document.Form1.<%=txtInvoi cePayment. ClientID%> .value;
What is the prob?
Please help
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(theF
{
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="validateTtlAmountP
</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.<%=txtPayme
var varPayingAmount = document.Form1.<%=txtInvoi
What is the prob?
Please help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this:
var varPaymentRequired = new Number(document.Form1.<%=t
var varPayingAmount = new Number(document.Form1.<%=t
And maybe also put in (for test purposes)
if (isNaN(varPayingAmount)) {
alert('varPayingAmount is not a number');
}