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

PHP function to convert numbers to strings of specified length

WBOY
Release: 2016-07-25 08:59:49
Original
2105 people have browsed it
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);
?>
Copy after login


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