Page 1 of 1

Automatic Decimal Placement

Posted: Tue Nov 02, 2010 2:56 pm
by djlex1928
Hi,

I have a price file in the following format:
320 = £3.20
2500 = £25.00
27380 = £273.80

Is there a php function that will automatically place the decimal correctly or should I just write a function to place the decimal 2 digits from the right?

Re: Automatic Decimal Placement

Posted: Tue Nov 02, 2010 3:14 pm
by AbraCadaver
The key is dividing by 100 and then telling it to add 2 decimal places:

Code: Select all

$pence = '320';

$pounds = sprintf("%01.2f", $pence / 100);
//or
$pounds = number_format($pence / 100, 2);

Re: Automatic Decimal Placement

Posted: Tue Nov 02, 2010 4:10 pm
by djlex1928
Thanks bud! :)

Re: Automatic Decimal Placement

Posted: Tue Nov 02, 2010 4:35 pm
by Jonah Bron
Two in a row? How is this possible?

Re: Automatic Decimal Placement

Posted: Wed Nov 03, 2010 11:17 am
by djlex1928
Lol a little different to my question but same principal. Gotta admit the odds on that are pretty low. :)