in reply to Re: Math::BigInt and Leading Zeros
in thread Math::BigInt and Leading Zeros

sprintf doesn't quite work - it can only handle whatever the underlying perl library handles natively:

$ perl5.8.6 -MMath::BigInt -e '$i = Math::BigInt->new("123_456_789_000 +_000"); printf "%025d\n", $i' -000000000000000000000001
The next best thing I can think of is:
$ perl5.8.6 -MMath::BigInt -e '$i = Math::BigInt->new("123_456_789_000 +_000"); $s = $i->bstr(); $s = '0' x (25 - length $s) . $s; print $s,$ +/' 0000000000123456789000000
Note that this does the right thing if the string is already longer than you want, e.g.:
$ perl5.8.6 -MMath::BigInt -e '$i = Math::BigInt->new("123_456_789_000 +_000"); $s = $i->bstr(); $s = '0' x (10 - length $s) . $s; print $s,$ +/' 123456789000000
Hope that helps.

Replies are listed 'Best First'.
Re^3: Math::BigInt and Leading Zeros
by hv (Prior) on May 05, 2005 at 09:38 UTC

    The underlying perl library natively handles strings fine - just change '%25d' to '%25s':

    zen% perl -MMath::BigInt -e '$i = Math::BigInt->new("123_456_789_000 +_000"); printf "%025s\n", $i' 0000000000123456789000000 zen%

    Hugo

      You may not have tried a big enough number.

      $ perl5.8.6 -MMath::BigInt -e '$i = Math::BigInt->new("123_456_789_000 +_000_000_000"); printf "%045d\n", $i' -00000000000000000000000000000000000000000001

        You're still trying to use decimals (%d), when the parent suggested using strings (%s):
        use Math::BigInt; my $i = Math::BigInt->new('123_456_789_000_000_000_000'); printf qq'%045s\n', $i; __OUTPUT__ 000000000000000000000000123456789000000000000

        radiantmatrix
        require General::Disclaimer;
        s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}