Trouble with javascript subtraction

Posted by rockinthesixstring on Stack Overflow See other posts from Stack Overflow or by rockinthesixstring
Published on 2010-03-21T05:54:15Z Indexed on 2010/03/21 6:01 UTC
Read the original article Hit count: 366

Filed under:
|
|

I'm working on a simple subtraction problem, but unfortunately it keeps returning NaN

Here is the function

   function subtraction(a, b) {
        var regexp = /[$][,]/g;

        a = a.replace(regexp, "");
        b - b.replace(regexp, "");


        var _a = parseFloat(a);
        var _b = parseFloat(b);

        return _a - _b;
     }

And here is how I'm calling it.

txtGoodWill.value = subtraction(txtSellingPrice.value, txtBalanceSheet.value);

The numbers that get submitted to the function are ONLY Currency (IE: $2,000 or $20, etc)

Now I know that I cannot subtract numbers with a $ or a ,, but I can't for the life of me figure out why they are getting evaluated in the equasion.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about subtraction