Jump to content

[SOLVED] Problem with PHP Calculation result.


nexus-cy

Recommended Posts

Hello Everyone!

First off I would like to wish you all a HAPPY NEW YEAR!!!

I have the following problem:

I am using the sum() and calculation functions to add respectively subtract several values from a mysql database. My script is working fine up-to the point when the calculated result doesn't contain a decimal number. For example if the result is 100.15 it is displaid correctly including decimal numbers and all. When the result is 100.00 it doesn't display the .00, now my question is: is it possible to display the .00 anyway and if so how can it be done?

on my result page I would like to see the decimal numbers even if they are empty because it looks better (in my opinion anyway).

at the moment i am using a script like follows to calculate my result:

1st: I query my database and retrieve the values using the "SELECT sum(credits) AS credits FROM..." & "SELECT sum(debits) AS debits FROM..."
2nd: I group my values as $sum1 & $sum2 using $sum1 = $result[credits]; & $sum2 = $result[debits];
3rd: I substract $sum2 from $sum1 using $total = ("$sum1" - "$sum2");
4th: I display the result using echo "$total";

what i am generally looking for is an if statement that would display the .00 decimals when the result is eg.: 100.00 and display normally if the result is eg.: 100.15

Suggestions on how to accomplish this would be very much appreciated!

Mike.
[quote author=kenrbnsn link=topic=120676.msg495233#msg495233 date=1167745993]
You should be using the [url=http://www.php.net/number_format]number_format()[/url] function.

Ken
[/quote]

Thank you for your reply, please be kind enough to explain respectively give an example of how to use this function.
[quote author=sasa link=topic=120676.msg495241#msg495241 date=1167746637]
try[code]echo number_format($total,2);[/code]
[/quote]

Thank you very much!

your example worked just fine, much appreciated.

Problem solved!

Greetings, Mike.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.