nexus-cy Posted January 2, 2007 Share Posted January 2, 2007 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.15Suggestions on how to accomplish this would be very much appreciated!Mike. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2007 Share Posted January 2, 2007 You should be using the [url=http://www.php.net/number_format]number_format()[/url] function.Ken Quote Link to comment Share on other sites More sharing options...
nexus-cy Posted January 2, 2007 Author Share Posted January 2, 2007 [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 Link to comment Share on other sites More sharing options...
sasa Posted January 2, 2007 Share Posted January 2, 2007 try[code]echo number_format($total,2);[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2007 Share Posted January 2, 2007 I gave you the link to the manual page where there are plenty of examples.Ken Quote Link to comment Share on other sites More sharing options...
nexus-cy Posted January 2, 2007 Author Share Posted January 2, 2007 [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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.