Quick Math question...

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
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Quick Math question...

Post by lazersam »

Anyone know how to turn one digit numbers like '1' into three digit numbers like '001' using a function? So, for example 81 would become 081.

Larry.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Look in the manual for the functions number_format, sprintf, etc...
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

its basically as simple as

Code: Select all

if ($var <= 9 && $var >= 0) {
    $var = "0".$var;
} elseif ($var <= 99) {
    $var = "00".$var;
}
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post by Revan »

Code: Select all

printf( "%04d", $strout_data );
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm.. has anyone ever checked which is faster?
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post by Revan »

Ambush Commander wrote:Hmm.. has anyone ever checked which is faster?
printf, also less redundant code with printf.
Post Reply