在PHP中用前导零来格式化一个数字 [英] Formatting a number with leading zeros in PHP
本文介绍了在PHP中用前导零来格式化一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量,它包含值 1234567
。
I have a variable which contains the value 1234567
.
我希望它包含8位数字,即 01234567
。
I would like it to contain exactly 8 digits, i.e. 01234567
.
是否有PHP函数?
Is there a PHP function for that?
推荐答案
使用 sprintf
:
Use sprintf
:
sprintf('%08d', 1234567);
您也可以使用 str_pad
:
Alternatively you can also use str_pad
:
str_pad($value, 8, '0', STR_PAD_LEFT);
这篇关于在PHP中用前导零来格式化一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文