How to format a string using the PHP function sprintf ()

Source: Internet
Author: User

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

Return Value: String

Function Type: Data Processing

The PHP function sprintf () is used to format strings. The format parameter is the conversion format, starting with the percent sign % to the conversion character. The format of the conversion includes

Fill in the blanks. If the value is 0, it indicates that the space is set to 0; if the value is 0, it indicates that the space is placed.
Alignment. The default value is right-aligned, and the negative number table is left-aligned.
Field width. Minimum width.
Precision. The number of digits after the decimal point.
For the sprintf () Type of the PHP function, see the following table % to print the percentage symbol without conversion.

The integer B is converted into binary.
The c integer is converted to the corresponding ASCII character.
D integer to decimal place.
F times the precision number to the floating point number.
O integer to octal.
The s integer is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.


Example of using PHP function sprintf ()

 
 
  1. <?
  2. $ Money1 = 68.75;
  3. $ Money2 = 54.35;
  4. $ Money = $ money1 + $ money2;
  5. // At this time, the variable $ money value is "123.1 ";
  6. $ Formatted = sprintf ("% 01.2f", $ money );
  7. // At this time, the variable $ formatted value is "123.10"
  8. ?>


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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.