Home > Article > Backend Development > How to make up for less than 10 digits in php
Completion method: 1. Use the "str_pad(numeric value, 10, "0", STR_PAD_RIGHT)" statement, and use zeros to supplement less than 10 digits; 2. Use "sprintf(" f", numerical variable)* pow(10,(10-strlen(numeric variable)))" statement, any less than 10 digits are padded with zeros.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php implementation is less than 10 Method of padding the digits with 0
1. Use the str_pad() function
str_pad() function to fill the string to the new length
Use the str_pad() function to return 10 digits, and add zeros if it is less than 10 digits.
<?php echo str_pad(12853,10,"0",STR_PAD_RIGHT)."<br>"; echo str_pad(1252,10,"0",STR_PAD_RIGHT)."<br>"; echo str_pad(1283365,10,"0",STR_PAD_RIGHT)."<br>"; ?>
Description: Possible values for the fourth parameter of this function:
STR_PAD_BOTH
- Fill Both sides of the string. If it's not an even number, the right side gets extra padding.
STR_PAD_LEFT
- pads the left side of the string.
STR_PAD_RIGHT
- Pads the right side of the string. This is the default.
2. Use the sprintf() function
Use the sprintf() function to format the value and convert it to A floating point number that specifies the number of decimal places.
Then multiply the decimal by (10 to the nth power) to convert it to 10 digits
<?php $c=12345; echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>"; $c=123; echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>"; $c=123366; echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>"; $c=1234452; echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to make up for less than 10 digits in php. For more information, please follow other related articles on the PHP Chinese website!