Home > Article > Backend Development > PHP function to convert numbers to strings of specified length
Introducing a function that can convert numbers into strings of specified length. Friends in need can refer to it.
Description: This function converts numbers into strings and specifies the length of the string. If the length is not long enough, add 0 on the left side. The code is as follows: <?php /** * 数字转为字符串 * edit bbs.it-home.org */ function num2str($num,$length){ $num_str = (string)$num; $num_strlength = count($num_str); if ($length > $num_strlength) { $num_str=str_pad($num_str,$length,"0",STR_PAD_LEFT); } return $num_str; } //调用示例 echo num2str(2,5); ?> |