Home > Backend Development > PHP Tutorial > How to use the PHP function sprintf() to format a string_PHP Tutorial

How to use the PHP function sprintf() to format a string_PHP Tutorial

WBOY
Release: 2016-07-15 13:30:13
Original
884 people have browsed it

Syntax: string sprintf(string format, mixed [args]...);

Return value: String

Function type: Information Processing

The PHP function sprintf() is used to format strings. The parameter format is the format of the conversion, starting with the percent sign % and ending with the conversion character. The converted formats include

fill-in-the-blank characters in sequence. If 0, it means that the blanks are filled with 0; blanks are the default value, which means that the blanks are left alone.
Alignment. The default is right-aligned, with negative tables aligned left.
Field width. is the minimum width.
Accuracy. Refers to the number of floating point digits after the decimal point.
For the type of PHP function sprintf(), see the table below. % prints out the percent sign without conversion.

b Convert integer to binary.
c Convert integer to corresponding ASCII character.
d Convert integer to decimal.
F times precision numbers are converted to floating point numbers.
o Convert integer to octal.
s Convert integer to string.
x integer is converted to lower case hexadecimal.
X Convert integer to uppercase hexadecimal.


Usage example of PHP function sprintf()

<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span> </span></span></li><li><span>$</span><span class="attribute">money1</span><span> = </span><span class="attribute-value">68</span><span>.75;  </span></li><li class="alt"><span>$</span><span class="attribute">money2</span><span> = </span><span class="attribute-value">54</span><span>.35;  </span></li><li><span>$</span><span class="attribute">money</span><span> = $money1 + $money2;  </span></li><li class="alt"><span>// 此时变量 $money 值为 "123.1";  </span></li><li><span>$</span><span class="attribute">formatted</span><span> = </span><span class="attribute-value">sprintf</span><span> ("%01.2f", $money);  </span></li><li class="alt"><span>// 此时变量 $ formatted 值为 "123.10"  </span></li><li><span class="tag">?></span><span>   </span></span></li></ol>
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446305.htmlTechArticleSyntax: string sprintf(string format, mixed [args]...); Return value: String function type : The data processing PHP function sprintf() is used to format strings. The parameter format is the converted format...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template