Usage of the built-in functions sprintf and number_format before the PHP number 0 (detailed description), sprintfformat

Source: Internet
Author: User

Usage of the built-in functions sprintf and number_format before the PHP number 0 (detailed description), sprintfformat

Most of the time, we need to format the number. For example, if the number of digits is less than the first digit, we need to add 0. PHP can be easily implemented, because PHP comes with related functions.

<? Php // generate 4-digit number, less than 0 $ var = sprintf ("% 04d", 2); echo $ var; // The result is 0002 echo date ('y _ m_d ', time ()). '_'. sprintf ('D', rand (444); echo sprintf ('% 05s',); // output 5 as a string, not enough to add 0?>

Sprintf () function

1. Syntax

Sprintf (format, arg1, arg2, arg ++)

Parameters Description
Format Required. Conversion format.
Arg1 Required. Specifies the parameter inserted at the first % symbol in the format string.
Arg2 Optional. Specifies the parameter inserted at the second % symbol in the format string.
Arg ++ Optional. Specifies the parameters inserted to the third, fourth, and other % symbols in the format string.

2. Description

The format parameter is the conversion format. It starts with the percent sign ("%") and ends with the conversion character. The following possible format values:

• %-Return percent sign
• % B-binary number
• % C-Characters Based on ASCII values
• % D-Signed decimal number
• % E-resumable counting (for example, 1.5e + 3)
• % U-Unsigned decimal number
• % F-floating point number (local settings aware)
• % F-floating point number (not local settings aware)
• % O-octal values
• % S-string
• % X-hexadecimal (lowercase letter)
• % X-hexadecimal (uppercase letters)

Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.

<?php  $number = 123;  $txt = sprintf("%f",$number);  echo $txt;  ?> 

3. Format number number_format ()

<?php  $number = 1234.56;// english notation (default)$english_format_number = number_format($number);// 1,235// French notation$nombre_format_francais = number_format($number, 2, ',', ' ');// 1 234,56$number = 1234.5678;// english notation without thousands seperator$english_format_number = number_format($number, 2, '.', '');// 1234.57?> 

The usage of the built-in functions sprintf and number_format provided by the above PHP numbers (detailed description) is all the content shared by Alibaba Cloud. I hope to give you a reference, we also hope that you can support the customer's home.

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.