Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    calculating to correct decimal for currency

    i am creating a scrip to calculate price, totals extra for an order form.

    right now the script round to the nearst 100 decimal place.

    my problem is that if the total is 56.80
    it appears as 56.8

    how can i make sure it tacks on that zero when it is needed?

    this is my bit of code..
    ___________________________________________________
    // check to make sure quantity and price are both NOT not a number...
    if (!isNaN(qnumber) && !isNaN(pnumber))
    {
    // multiply quanity and price
    tnumber = (qnumber * pnumber);
    tnumber = Math.round(tnumber * 100) / 100;
    tcommand = "document.form.aidtotal"+row+".value = tnumber";
    eval(tcommand);
    aidsubtotal += tnumber
    }
    // and make sure they aren't blank
    else if ((qvalue != "") && (pvalue != ""))
    {
    alert("Please enter a number");
    }
    else
    {
    tcommand = "document.form.aidtotal"+row+".value = 0";
    eval(tcommand);
    }

    __________________________________________________


    does any one have any suggestions?
    i feel like the answer is probably simple enough. i just cant seem to wrap my brain around it...

    Thanks you in advance for any help you can offer!!

    lhenry
    lhenry@quinstreet.com

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    yorkshire, uk
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    ...
    tnumber = Math.round(tnumber * 100) / 100;
    if (10 * tnumber == Math.round(10 * tnumber))
      tnumber += "0";
    ...
    should do it.
    neil.c

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or try

    tnumber = tnumber.toFixed(2);

  • #4
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    the final solution

    thank you for your replies!
    both of them worked. but not as completely as i need it to.

    someone else sent me this little function whish works perfectly for my purposes.

    function displayDollars(number) {
    dollars = Math.floor(number);
    cents = Math.round(number*100) - dollars*100;
    if (dollars == 0) {
    if (cents < 10) {
    return "0.0" + cents;
    } else {
    return "0." + cents;
    }
    } else {
    if (cents < 10) {
    return dollars + ".0" + cents;
    } else {
    return dollars + "." + cents;
    }
    }
    }

    so then i just ran tnumber through this function. and taadaaa!

    just thought i would post that solution incase anyone else could use it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •