Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running Total issues 2

Status
Not open for further replies.

skicamel

Programmer
Dec 24, 2001
126
US
This may be a stupid question. PHP is not my strong suit. I'm trying to put a total at the bottom of a query result w/ no success. It wasn't working so I added in a running total to see where it hung up. This example works fine but doesn't format properly:

$grand = 0;
while (odbc_fetch_into($result,$myrow))
{
$city = $myrow[0];
$state = $myrow[1];
$total =$myrow[2];
$grand = $grand + $total;
echo &quot;<tr><td>$city</td><td>$state</td><td>$total</td><td>$grand</td></tr>&quot;;
}

The goal was to have all of the totals in a money format. I changed the pull like this:

$grand = 0;
while (odbc_fetch_into($result,$myrow))
{
$city = $myrow[0];
$state = $myrow[1];
$total =number_format($myrow[2],2);
$grand = $grand + $total;
echo &quot;<tr><td>$city</td><td>$state</td><td>$total</td><td>$grand</td></tr>&quot;;
}

But this isn't working. With the running total, it appears that when it hits an amount over 1000, it takes what's before the comma... ex: It added up nicely through the first ten rows or so, then it hit an amount just over 12,000, and only added in 12 dollars.

--------------------
I just about posted this then an idea beat me on the back of my head. I'm more of a SQL guy, so I converted this to varchar before sending to PHP and it works like I'd planned, but now my curiosity is peaked. Can this be achieved in PHP? The format would arrive to the page w/ 4 places after the decimal point. How would one make these show up w/ 2 decimal places and total properly?

Again forgive my ignorance if this is blatantly simple. Thanks, all.
 
You have just hit upon one of the pitfalls of working in a 'weakly typed' language. The specific problem is here:
Code:
$total =number_format($myrow[2],2);
$grand = $grand + $total;
In a strongly typed language, such as C++ or Java, where types are strictly enforced, you would never be allowed to do $grand = $grand + $total;, because that tries to add data from two different types.

Remember, in PHP there is no such thing as a &quot;money&quot; type. &quot;money&quot; gets converted to floating point numbers, but these are juggled with integers whenever convenient. Thus 10.00 just becomes 10, and 12.10 becomes 12.1. Your $total values are being returned as floating points to PHP, and truncated to the significant decimal.

But, once you do number_format($myrow[2],2), you have converted that value to a string. See , and you will see
Code:
string number_format ( float number [, int decimals [, string dec_point [, string thousands_sep]]])
. Notice that it returns a string.

So, now you have a float value ($grand), being added to a string, even though it looks like 12.00, it is just a string. But since PHP is weakly typed, it does it's best to convert that string back to a numeric format to do the addition. Thus &quot;$grand + 12.00&quot; would work just fine, but &quot;$grand + 12,000.00&quot; would stop the conversion at the first non-numeric character (,), and end up with &quot;$grand + 12&quot;.

ANSWER: do number_format() only after you have done your math. It is for output formatting only:

$total = $myrow[2];
$grand = $grand + $total;
$output_grand = number_format($grand);

This way, you can keep updating the value of $grand with each row, but still output the formatted value at each row.

Maybe this is over-explained, but I hope it satisfied your curiosity ;-). -------------------------------------------

&quot;Now, this might cause some discomfort...&quot;
(
 
That was perfectly explained. Good show!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top