Zero-pad digits in string


Question

I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions

1
113
11/29/2008 6:05:44 PM

First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:

$s = sprintf('%02d', $digit);

For more information, refer to the documentation of sprintf.

202
4/2/2016 11:24:59 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon