Home  >  Article  >  Backend Development  >  PHP function to convert numbers to strings of specified length

PHP function to convert numbers to strings of specified length

WBOY
WBOYOriginal
2016-07-25 08:59:492059browse
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);
?>


Statement:
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