# Thread: converting to 2 decimal places in php

1. Hello again,

Another quickie....

How do I convert a number with many decimal places, to just 2 decimal places please (i.e. like with prices)?

e.g. \$total = 1.222222;

so how would I make \$total = 1.22

Thanks,

Jason

2. Hi Jason,

I had (almost) the same problem. This thread may be able to help you out:

Andrew.
http://www.mammoth.com.au/

3. hmmmmm not quite,

Thanks insomniac, but I think this will remove all decimal places!!!

I'd like to keep 2 of them, e.g. £14.33

So any other ideas please?

Regards,

Jason

4. Jason,

I know that if you play around with that code in the other thread, you can get the result your looking for.

Andrew.
http://www.mammoth.com.au/

5. \$var = sprintf( "%.2f", \$var );

Should return \$var to 2 decimal places.

6. perhaps

\$value = 14.8366723786;
// change to whatever value...

\$value = \$value * pow(10, 3);

\$value = floor(\$value);

\$value = (float) \$value/10;
(float) \$modSquad = (\$value - floor(\$value));
\$value = floor(\$value);
if (\$modSquad > .5){
\$value++;
}

\$value = \$value / (pow(10, 2));

okay.. I was beaten to it.. and my way is sooooooo complicated but never mind

<Edited by PeterW on 01-10-2001 at 11:28 AM>

7. thanks guys!!!

8. There is a function in php for this

\$var = number_format(\$var, 2);

where number_format converts the given string to the number of decimal point specified, in this case 2

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•