Home > Backend Development > PHP Tutorial > Introduction to two functions of PHP to implement digital zero padding_PHP tutorial

Introduction to two functions of PHP to implement digital zero padding_PHP tutorial

WBOY
Release: 2016-07-13 10:29:52
Original
862 people have browsed it

There are at least two functions in PHP that can help us quickly implement digital zero padding:
The first is the PHP str_pad function:

Copy code Code As follows:
#str_pad — Use another string to fill a string to the specified length

As the name suggests, this function is for strings, filling the specified string with any other string
str_pad parameter description:
Copy code The code is as follows:
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
#Common parameter description: str_pad (string with padding, length after padding, padding string, padding position)

The padded length It must be a positive integer. There are three options for filling the position,
Left side: STR_PAD_LEFT
Right side: STR_PAD_RIGHT
Both ends: STR_PAD_BOTH
Example display:
Copy code 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

A detail worth noting in the above example Yes, if the number of padded digits is an odd number, for example, in Example 3, 7 0s are padded, the right side takes precedence.

Let’s look at another way to add zeros:
PHP sprintf function:

Copy code The code is as follows:
#sprintf — Returns a formatted string

This function is more flexible to use and needs to be explored by scholars. Here we mainly talk about the processing method of padding zeros on the left side of the value (or padding zeros after the decimal point);
First look at padding zeros on the left
Copy the code The code is as follows:
echo sprintf("%05d",1);
# The meaning of %05d: Use a 5-digit number to format the following parameters. If there are less than 5 digits, add zeros
# The running result is 00001

Look at the decimal point and add zeros
Copy code The code is as follows:
echo sprintf("%01.3f",1);
# %01.3f means: use a decimal point At least three digits after the decimal point and less than three digits are padded with zeros, and there is at least one digit before the decimal point. For floating point numbers that are less than one digit padded with zeros, format the following parameters
#. The running result is: 1.000

In addition, you can also write one yourself Customize functions for processing;
Every writing code has its advantages and disadvantages, you can choose the one that suits you best;

Note: sprintf can ensure that 1 will not be added to 1000000 by mistake, and str_pad can ensure that you add whatever you want.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769240.htmlTechArticleThere are at least two functions in PHP that can help us quickly implement digital zero padding: The first is the PHP str_pad function: Copy The code code is as follows: #str_pad — Use another string to pad the characters...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template