On Fri, 26 Nov 2004 17:47:25 +1100, David Clarke wrote:
> my $amount1 = 14313562897;
> my $amount2 = 00000013625;
> $amount = sprintf("%011d", $amount1 + $amount2);
> print $amount."\n";
>
> The answer perl gives me is -0000000001
> How can this be ?
>
> As well, if the large number starts with 0, I get an "Illegal octal
> digit '8' at test.pl line 2, at end of line". As though perl cant
> handle numbers bigger than 10 digits or so.
You might want to read up on
a) sprintf, as provided in the perldoc
sprintf FORMAT, LIST
Returns a string formatted by the usual printf conventions of
the C library function sprintf. See below for more details and see sprintf(3)
or printf(3) on your system for an explanation of the general principles.
b) the format sizes that sprintf returns
%d a signed integer, in decimal
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%e a floating-point number, in scientific notation
%f a floating-point number, in fixed decimal notation
c) the numbers that fit into each of the format sizes. Example given
is for signed int. A signed int can hold -1 or up to 4294967295.
Now, when we look at your value:
14_313_562_897
and the biggest value containable in a signed integer
4_294_967_295
we notice that your value is bigger than the biggest containable value, and as
such will be set to the next value: -1.
So, everything is fine with Perl. You might want to use 'f' and unformat the
comma and the zeros after it, to reach your goal.
thanks
/oliver/
--
"If you believe everything you read, you better not read." -Japanese Proverb
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>