Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number_format

Status
Not open for further replies.

curvv

IS-IT--Management
Jan 11, 2001
103
ZA
Hi

I would like to know how to format a number ex: 3 to display 003.

Any help will be appreciated.

thanx
 
You'll need to use either "printf"(if you just want to print a formated number) or "sprintf"(if you want to format a number and save that formatted number in a variable). Both functions use the same formatting options. Go to and search for "sprintf".

I think this does the trick:

<?
$a = 3;
echo &quot;Start with number = $a<br><br>&quot;;

$formated_num = sprintf(&quot;%03d&quot;, $a);
echo &quot;\$a formated with sprintf(\&quot;%03d\&quot;, \$a) = $formated_num<br>&quot;;
?>

Prints:
-----

Start with number = 3

$a formated with sprintf(&quot;%03d&quot;, $a) = 003

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top