javascript subtract to dollar values

Posted on 2012-08-19
Last Modified: 2012-08-20
$a = document.getElementByID('dollar1');  //  returns $2,000,000.50;
$b = document.getElementByID('dollar2');  //  returns $1,000,000.10;

How do I get $a - $b to return $1,000,000.40;

I've tried parseFloat($a) - parseFloat($b)
I've tried parseFloat($a.replace('$', '')) - parseFloat($a.replace('$', ''))

Question by:joomla
    LVL 82

    Expert Comment

    by:Dave Baldwin
    Like this...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    <title>JS Math</title>
    <h1>JS Math</h1>
    <script type="text/javascript">
    var d1 = 2000000.50;
    var d2 = 1000000.10;
    var d3;
    d3 = d1-d2;
    // -->

    Open in new window


    Author Comment

    sorry, I don't understand what you're suggesting.

    I have a form with two three fields
    If I enter the value $2,000,0000.50 into field called "dollar1"
    enter the value $1,000,000.10 into a field called "dollar2"

    I want an onchange routine in field "dollar2" to populate a field called "dollar3"
    with the value $1,000,000.40

    LVL 82

    Expert Comment

    by:Dave Baldwin
    The reason you don't often see numbers formatted like that is that javascript doesn't understand the commas ','.  If you enter numbers that way, you have to strip off the '$' and the ',' before you try to do the math or it won't work.  Then to display the result that way, you have to add those back into the result.

    Author Comment

    i'm sorry to be ackward, but I did try strip the $ and the , from the numbers but keep getting an error.

    can you suggest a work reg pattern ?
    LVL 7

    Expert Comment

    Try this script.

    <script type="text/javascript">
    function getVal(){
    var dollar1= parseInt(document.getElementById('dollar1').value.replace('$', ''));
    var doller2= parseInt(document.getElementById('dollar2').value.replace('$', ''));
    var ans= doller1- doller2;

    Open in new window

    I think this may helps you.
    If not then refer this link

    LVL 75

    Accepted Solution

    1) remove $ and ALL commas
    2) subtract the numbers
    3) reformat the numbers

    var diff = parseFloat($a.replace(/[,$]/g, '')) - parseFloat($b.replace(/[,$]/g, ""))
    var res = "$"+addCommas(diff);

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
    This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    746 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

    17 Experts available now in Live!

    Get 1:1 Help Now