quiztoon — 2013-01-08T11:51:53-05:00 — #1
I have created a very basic database which records my appointments against my purchases.
I want to know what my conversion is, to that end i am using the following, however I want it to only display the result to 2 decimal places.
$conversion = $purchases_total / $appts * 100;
$purchases_total is made up of adding 2 variables together, these variables get the info direct from the database. $appts is simply a count of the total entries.
Any assistance in this would be greatly appreciated.
I cant seem to find anything on google or within the forum which fits the bill. I am guessing its something simple but am stuck.
guido2004 — 2013-01-08T12:04:07-05:00 — #2
Googling for php decimal places gave me this: http://php.net/manual/en/function.number-format.php
Does that solve your problem?
quiztoon — 2013-01-08T12:10:53-05:00 — #3
I am sure that does solve my problem. I did find that page myself but I don't understand what is telling me to do, or where to put the formatting.
Thats why I came here, usually get things explained in a more simpler fashion so I understand what to do.
cpradio — 2013-01-08T14:13:49-05:00 — #4
You will want to use it like so:
$outputTotal = number_format($conversion, 2);
If you need to change the thousands separator or the decimal separator (due to not working in US currency, or a foreign currency that uses a similar definition) then be sure to supply those additional parameters
$outputTotal = number_format($conversion, 2, ',', '.');
quiztoon — 2013-01-08T14:21:56-05:00 — #5
Many thanks, the bit I wasn't getting was creating another variable, now I see the answer I can see it in the examples at the site above.
Sometimes I get a bit bogged down with the jargon and I am not sure what I am putting where.
As always very grateful for your assistance
starlion — 2013-01-09T08:45:03-05:00 — #6
Number_format returns a string (note: You can see that in the manual page - the return type is listed in the function definition before the function name.), so it's also perfectly acceptable to call this function amid an echo with appropriate concatenation;
echo "My number is ".number_format($somenumber,2)." so there."
paulinetaylor85 — 2013-01-11T05:58:13-05:00 — #7
The two methods through which you can sort your query is
1) return number_format((float)$number, 2, '.', '');
2) $padded = sprintf('%0.2f', $unpadded);
cups — 2013-01-12T04:52:44-05:00 — #8
Is the OP talking about money? money_format()