[Thamesvalley-pm] Leading 0s problem with large integers

Smylers Smylers at stripey.com
Tue Jan 6 05:38:59 PST 2009


Duncan Kendall writes:

>   $string = sprintf("%0*d", 10, $number);
> 
> And this works just fine for values of $number up to 2147483647
> ((2^31)-1). Changing it to:
> 
>   $string = sprintf("%0*u", 10, $number);
> 
> improves things and works fine for anything up to ((2^32)-1).
> 
> I can get around this wit a bit of string manipulation, calculating
> the number of digits and then manually inserting 0s at the start, but
> that isn't very elegant.  Can anyone offer up a neat and simple
> solution?

Why does sprintf need to know that it's an integer?  Using %0*s appears
to do what you want.

Smylers


More information about the Thamesvalley-pm mailing list