Why is this not adding like I think it should..??

Posted on 2007-10-21
Last Modified: 2007-10-21
I've got the following within a script:

$ItemTotal = number_format($_POST['ItemTotal'],2);
$SalesTax = round($ItemTotal * ($SalesTaxRate / 100),2);
$HandlingTotal = number_format($_POST['HandlingTotal'],2);
$OrderTotal = round($ItemTotal + $SalesTax + $HandlingTotal, 2);

Then in my form I'm putting 1240.00 for $ItemTotal, $SalesTax and $HandlingTotal are both 0.00 right now.  $OrderTotal windes up giving me just 1 instead of $1,240.00 like I'm expecting.  What am I missing here..??
Question by:Andrew Angell
    LVL 13

    Accepted Solution

    number_format(1240.00, 2) returns the string "1,240.00"
    Later, when you use $ItemTotal again, it treats $ItemTotal as its integer equivalent because of the addition operator. The integer equivalent of '1,240.00' is 1. Just don't use number_format there. Use it on $OrderTotal.
    LVL 10

    Assisted Solution

    When you use number_format, the result is a string, instead of a number. This means that you can't do calculations with it. So what you should do is make the formatting the last step, after calculating all the numbers.

    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!

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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.

    761 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

    9 Experts available now in Live!

    Get 1:1 Help Now