The realization of the function of the digital zero-complement in PHP

Source: Internet
Author: User
Tags sprintf

There are at least two functions in PHP that can help us quickly achieve digital 0:

The first is the PHP str_pad function:

The code is as follows:

#str_pad-Fills a string with another string for a specified length
As the name suggests, this function is for strings, filling any other string with the specified string

Str_pad parameter Description:

The code is as follows:

String Str_pad (string $input, int $pad _length [, string $pad _string = "" [, int $pad _type = str_pad_right]])
#常用参数说明: Str_pad (with filled string, filling length, filling string, filling position)
The length must be a positive integer, filling position has three options,
Left: Str_pad_left
Right: Str_pad_right
Both ends: Str_pad_both
Example Show:

The code is as follows:
Echo Str_pad (1,8, "0", str_pad_left);
#结果: 00000001
Echo Str_pad (1,8, "0", str_pad_right);
#结果: 10000000
Echo Str_pad (1,8, "0", Str_pad_both);
#结果: 00010000
$pad =str_pad (($number [0]["Count (*)"]+1), 4, "0", str_pad_left); Auto Fill 0

One notable detail in the above example is that if the number of digits that are filled is odd, for example three to fill 7 0, the right is preferred.

Here's another way to make a zero:

The

sprintf () function
does not feel much like the C language
1. Syntax
sprintf (format,arg1,arg2,arg++)
parameter   description
format  required. Conversion format.
arg1  required. Specify the parameter to be inserted into the first% symbol in the format string.
arg2  Optional. Specify the parameter to be inserted into the second% symbol in the format string.
arg++  Optional. Specify the parameters to be inserted into the format string at the third to fourth and so% symbols. The
2. Description
Parameter format is the format of the transformation, starting with the percent sign ("%") to the end of the conversion character. The following possible format values:
percent%-return symbol
%b-binary
%c-characters by ASCII value
%d-Signed decimal
%e-Renewable count (for example, 1.5e+3)
%u- unsigned decimal digits
%f-floating-point numbers (Local settings Aware)
%f-floating-point number (not local settings aware)
%o-eight number
%s-string
%x-16 in Number (lowercase)
%x-16 (uppercase)
Arg1, arg2, + + etc parameters will be inserted into the main string in percent (%) Symbol Place. The function is executed incrementally. In the first% symbol, insert arg1, at the second% symbol, insert arg2, and so on.
<?php  
$number = 123;  
$txt = sprintf ("%f", $number);  
Echo $txt; &n bsp; 
?>
3. Format number Number_format ()
<?php  
$number = 1234.56;

中文版 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;

中文版 notation without thousands seperator
$english _format_number = Number_format ($number, 2, '. ', ');
1234.57
?>

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.