Introduction to two functions for implementing the digit zero completion function in PHP

Source: Internet
Author: User
This article mainly introduces two functions for implementing the digit zero-padding function in PHP. For more information, see at least two functions in PHP to help us quickly implement the digit zero-padding function:
The first is the PHP str_pad function:
The code is as follows: # str_pad-use another string to fill the string with the specified length
As the name implies, this function is intended for strings and fills in any other strings for 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])
# Description of common parameters: str_pad (string with padding, the length after filling, the string to fill, and the position to fill)
The length after filling must be a positive integer and there are three options to fill the position,
Left: STR_PAD_LEFT
Right: STR_PAD_RIGHT
Two ends: STR_PAD_BOTH
Instance display:
The code is as follows: echo str_pad (1,8, "0", STR_PAD_LEFT );
# Result: 00000001
Echo str_pad (1,8, "0", STR_PAD_RIGHT );
# Result: 10000000
Echo str_pad (1,8, "0", STR_PAD_BOTH );
# Result: 00010000
In the above example, it is worth noting that if the number of digits to be filled is an odd number, for example, if the number of digits to be filled is 7 in the third case, the right side is given priority.

Next, let's look at another method to add zero:
PHP sprintf function:
The code is as follows: # sprintf-returns a formatted string
This function is flexible to use and needs to be explored by scholars. here we mainly discuss how to add zeros to the left of the value (or add zeros after the decimal point;
First look at the left zero
The code is as follows: echo sprintf ("% 05d", 1 );
# % 05d: format the following parameter with a 5-digit number. if there are less than five digits, add zero.
# The running result is 00001.
Then add zero after the decimal point.
The code is as follows: echo sprintf ("% 01.3f", 1 );
# % 01.3f: use a floating-point number with at least three digits after the decimal point to complete zero, at least one digit before the decimal point, and less than one complement zero to format the following parameters.
# Running result: 1.000
You can also compile a UDF for processing;
Code writing is both superior and inferior. you can choose the appropriate one;

Note: sprintf can ensure that 1 is not supplemented into 1000000 by misoperations, and str_pad can ensure that what needs to be supplemented.

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.