Page 1 of 1

Quick Math question...

Posted: Sat Jun 04, 2005 10:31 am
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.

Posted: Sat Jun 04, 2005 10:40 am
by timvw
Look in the manual for the functions number_format, sprintf, etc...

Posted: Sat Jun 04, 2005 1:10 pm
by AGISB
its basically as simple as

Code: Select all

if ($var <= 9 && $var >= 0) {
    $var = "0".$var;
} elseif ($var <= 99) {
    $var = "00".$var;
}

Posted: Sat Jun 04, 2005 1:20 pm
by Revan

Code: Select all

printf( "%04d", $strout_data );

Posted: Sat Jun 04, 2005 3:36 pm
by Ambush Commander
Hmm.. has anyone ever checked which is faster?

Posted: Sat Jun 04, 2005 3:44 pm
by Revan
Ambush Commander wrote:Hmm.. has anyone ever checked which is faster?
printf, also less redundant code with printf.