compare double values in javascript

Posted on 2007-07-23
Last Modified: 2008-01-09
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
Question by:tia_kamakshi
    LVL 11

    Expert Comment

    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');
    LVL 3

    Accepted Solution

    I think your problem is that you are comparing the numbers as string...
    var varPaymentRequired = parseFloat('77249.83');
    var varPayingAmount = parseFloat('44444444444');

    Should work...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now