in reply to perls long number problem

You first example is very confused - you try to use a number of uninitialised variables. By changing it to:

my $som_rekening = "2147483648"; my $pad_len_sum = 15; my $padded_som = sprintf("%0${pad_len_sum}s", $som_rekening); print "PADDED SOM: $padded_som - $som_rekening\n";

I believe I've now worked out what the problem is - it prints out:

PADDED SOM: 000002147483647 - 2147483648

It looks like you're running up against the limits of Perl's numeric accuracy. You might find that that Math::BigInt module will allow you to deal with larger numbers.

If, on the other hand, you are formatting this number to display it in some way, why not just treat it as a string like this:

my $som_rekening = "2147483648"; my $pad_len_sum = 15; my $padded_som = sprintf("%0${pad_len_sum}s", $som_rekening); print "PADDED SOM: $padded_som - $som_rekening\n";

This outputs:

PADDED SOM: 000002147483648 - 2147483648
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: perls long number problem
by toadi (Chaplain) on Dec 10, 2001 at 16:29 UTC
    Yep that was what I wanted to show, Perl just couldn't handle the long numbers. I wanted to see if there's another solution than trying to handle numbers as string...

    I made some typing mistakes in the code, Sorry for that.

    --
    My opinions may have changed,
    but not the fact that I am right

      Perl can only handle integers up to the size it was configured with. But it can handle numbers larger that that. Try your example with

      sprintf("%{$len}.0f",$number);

      And you will see the number just fine. This is because perl uses doubles to hold real numbers, which can typically hold numbers of more bits. On most machines perl runs, integers are 32 bits and doubles use 53 bits for the mantissa.

      So yes, perl can handle larger numbers without strings, providing you don't convert them to integers