zerofill整数php

时间:2009-09-18 10:02:17

标签: php integer fill zero autofill

我如何进行'零填充'整数?

1 becomes 0001
40 becomes 0040
174 becomes 0174

2 个答案:

答案 0 :(得分:68)

$filled_int = sprintf("%04d", $your_int)

答案 1 :(得分:22)

$number = 12;
$width = 4;
$padded = str_pad((string)$number, $width, "0", STR_PAD_LEFT);