Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zerofill a integer php

How would I go about 'zero filling' an integer?

ie

1 becomes 0001
40 becomes 0040
174 becomes 0174
like image 808
dotty Avatar asked Sep 18 '09 10:09

dotty


2 Answers

$filled_int = sprintf("%04d", $your_int)
like image 110
Palantir Avatar answered Oct 17 '22 11:10

Palantir


$number = 12;
$width = 4;
$padded = str_pad((string)$number, $width, "0", STR_PAD_LEFT); 
like image 29
Joe Avatar answered Oct 17 '22 12:10

Joe