★ wanayoo — archive 1999 http://www.php.net/manual/function.sprintf.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: sprintf
View the source code for this pageSearch the site



Previous page
 soundex
 Updated
Sat, 12 Aug 2000
strcasecmp 
Next page


sprintf

(PHP3 , PHP4 )

sprintf -- Return a formatted string

Description

string sprintf (string format [, mixed args...])

Returns a string produced according to the formatting string format.

The format string is composed by zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf() and printf().

Each conversion specification consists of these elements, in order:

  1. An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). See the examples below.

  2. An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified.

  3. An optional number, a width specifier that says how many characters (minimum) this conversion should result in.

  4. An optional precision specifier that says how many decimal digits should be displayed for floating-point numbers. This option has no effect for other types than double. (Another function useful for formatting numbers is number_format().)

  5. A type specifier that says what type the argument data should be treated as. Possible types:

    % - a literal percent character. No argument is required.
    b - the argument is treated as an integer, and presented as a binary number.
    c - the argument is treated as an integer, and presented as the character with that ASCII value.
    d - the argument is treated as an integer, and presented as a decimal number.
    f - the argument is treated as a double, and presented as a floating-point number.
    o - the argument is treated as an integer, and presented as an octal number.
    s - the argument is treated as and presented as a string.
    x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
    X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

See also: printf(), sscanf(), fscanf(), and number_format().

Examples

Example 1. Sprintf(): zero-padded integers


$isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day);
      

Example 2. Sprintf(): formatting currency


$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf ("%01.2f", $money);
// echo $formatted will output "123.10"
      


User Contributed Notes: sprintf


paolini@kahuna.sdsu.edu
14-Apr-1999 01:12
Hey folks, don't forget to prefix a precision specifier with a period '.'!
Thus, to print a floating point number,
say $x, with two digits after the decimal point you would write:

<PRE>
printf( "%.2f", $x );
</PRE>



guenther.mair@web.by.com
07-Jul-1999 12:37
to add a percent sign, substitute every % with %% ;)


kris@mha.ca
21-Sep-1999 07:27
Left justify sprintf:

<PRE> sprintf("%-100s",$txt);</PRE>

Kris



chaizzilla@hotmail.com
20-Oct-1999 03:03
does sprintf() have a means to also determine the maximum charachters the conversion results in? i'm trying to ditch tables.. "3.An optional number, a width specifier that says how many characters (minimum) this conversion should result in."


robertk@robertk.com
20-Nov-1999 12:37
Here's a function which will format numbers with commas where necessary, and optionally add a dollar sign.

<pre>
function Money($inbuffer, $dolsign = 0)
{
$buffer = sprintf("%0.2f", $inbuffer);
if(strlen($buffer) > 6)
{
$thenumber = "";
$decimal = substr($buffer, strrpos($buffer, "."));
$left = substr($buffer, 0, strrpos($buffer, "."));
while(strlen($left) > 3)
{
$thenumber = $thenumber . "," . substr($left, -3);
$left = substr($left, 0, strlen($left)-3);
}
$thenumber = $left . $thenumber . $decimal;
}
else
$thenumber = $buffer;
if($dolsign != 0)
$thenumber = "\$ $thenumber";
return($thenumber);
}
</pre>



scott@mha.ca
01-Dec-1999 05:38
if you want to format a date, you should NOT be using sprintf(), you should be using date() ..


bschuetz@affinity.net
03-Dec-1999 02:59
Actually if you want to format numbers with commas a much easier method would be to just use the number_format() function.


dean@av8.com
09-Feb-2000 11:12
I need to format an unsigned number for a sql query eg sprintf("select servername from x where ip > '%d' and ip < '%d'", 130<<24|105<<16|11<<8, 130<<24|105<<16|11<<8|255);
in php3: the value is printed as -2107044865

perl -e 'print 130<<24|105<<16|11<8|255'
2187919615

What to do? Don't want to rewrite everything in Java... Really __need__ to have unsigned numbers...



olip@xzone.pl
21-Feb-2000 12:39
if U need counter do it like that:

printf("%07d",$counter_value);

U will get eg. 0000005 or 0000150 dependign on the $counter_value

If u wanna have counter longer or shorter change the 7 to whatever U need.



tim_converse@excite.com
11-Jul-2000 02:00
Conversion specifications begin with a percent sign (%). (This is obvious from the examples, but the explanation does not say so directly.)



alec@brainpod.com
01-Aug-2000 01:11
This is my function for converting mysql dates into dates to be passed to the php date function...

<pre>
cfunction mysqlDate($date) {
ereg("(....)(..)(..)(..)(..)(..)",$date,$reg);
return mktime($reg[4],$reg[5],$reg[6],$reg[2],$reg[3],$reg[1]);
}

echo date("M d Y",mysqlDate($somedb->f('datefield'));
</pre>


This is a good function to keep in all your db code..



 About Notes


Previous page
 soundex
 Updated
Sat, 12 Aug 2000
strcasecmp 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.