Automatic Decimal Placement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djlex1928
Forum Commoner
Posts: 31
Joined: Sun Sep 19, 2010 3:23 pm

Automatic Decimal Placement

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Automatic Decimal Placement

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
djlex1928
Forum Commoner
Posts: 31
Joined: Sun Sep 19, 2010 3:23 pm

Re: Automatic Decimal Placement

Post by djlex1928 »

Thanks bud! :)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Automatic Decimal Placement

Post by Jonah Bron »

Two in a row? How is this possible?
djlex1928
Forum Commoner
Posts: 31
Joined: Sun Sep 19, 2010 3:23 pm

Re: Automatic Decimal Placement

Post by djlex1928 »

Lol a little different to my question but same principal. Gotta admit the odds on that are pretty low. :)
Post Reply