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

compare double values in javascript

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.


<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;


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

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

Should work...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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