views:

561

answers:

2

Hay, how would i go about 'zero filling' an integer?

ie

1 becomes 0001
40 becomes 0040
174 becomes 0174

Thanks

+1  A: 
$number = 12;
$width = 4;
$padded = str_pad((string)$number, $width, "0", STR_PAD_LEFT);
Joe
This pads to the right of the number, and pads with spaces not zeros. $padded = str_pad($number, $width, 0, STR_PAD_LEFT);
Tom Haigh
Yeah, ommission on my part. Corrected. But I would retain some semblance of type safety by providing appropriate types (even if conversion is automatic).
Joe
yeah that is a good idea, not sure why i took it out.
Tom Haigh
+10  A: 
$filled_int = sprintf("%04d", $your_int)
Palantir
Also http://php.net/manual/en/function.str-pad.php, just as a sidenote.
nikc