Decimal places

Hi all,

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.

many Thanks

Googling for php decimal places gave me this: http://php.net/manual/en/function.number-format.php
Does that solve your problem?

Hi guido,

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.

Thanks

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, ',', '.');

cpradio.

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

many Thanks

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.”

The two methods through which you can sort your query is

  1. return number_format((float)$number, 2, ‘.’, ‘’);

  2. $padded = sprintf(‘%0.2f’, $unpadded);

Is the OP talking about money? money_format()