sprintf in PHP differs from printf function usage

Source: Internet
Author: User
Tags mixed printf sprintf string format

  This article mainly describes the PHP sprintf and printf function usage difference analysis, need friends can refer to the following

Here's an example: rounding the two-bit     code after the decimal point is as follows: <?php $num 1 = 21; Echo sprintf ("%0.2f", $num 1). " <br/> "; Output 21.00 $num 2 = 16.3287; Echo sprintf ("%0.2f", $num 2). " <br/> "; Output 16.33 $num 3 = 32.12329; Echo sprintf ("%0.2f", $num 3). " <br/> "; Output 32.12 ?>       explanation%0.2f meaning:  % indicates that the starting character 0 indicates that the vacancy is filled with 0 2, indicating that the decimal point must occupy two-bit f to convert to floating-point number   &NBS P Convert character =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-% print percent symbol, do not convert.   b integers are converted to binary.   c integers into the corresponding ASCII characters. The   D integer is turned into 10.   F-Times precision numbers are converted to floating points. The   o integer is converted into octal. The   s integer is converted into a string.   x integers are converted to lowercase 16.   X integers are converted to uppercase 16.   The difference between printf and sprintf   1. printf function:   int printf (string format [, mixed args [, mixed ...]])   produces output according to format, WH The ICH is described in the documentation for SPRINTF ().   Returns The length of the outputted string.   Format the text after the output, such as:     code as follows: $name = "Hunte";  $age =25;  printf ("My name was%s, age%d", $name, $age); nbsp     2. sprintf function: &NBsp String sprintf (string format [, mixed args [, mixed ...]])   Returns A string produced according to the formatting string format.   is similar to printf, but does not print, but instead returns formatted text, and others are the same as printf.   3. Print function:   is a function that can return a value with only one argument.   int print (string arg)   outputs Arg. Returns 1, always.    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.