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

Duncan Kendall dk-thamesvalley-pm at bdksol.co.uk
Tue Jan 6 05:15:16 PST 2009


I have a bit of code that is trying to format a number as a 10 digit
string with leading 0s inserted as necessary.

The code is:

  $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).

The system does not have 64 bit support, and so trying to persuade Perl
that $number is a long long doesn't help.

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?


More information about the Thamesvalley-pm mailing list