Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sprintf a big number in Perl?

On a Windows 32-bit platform I have to read some numbers that, this was unexpected, can have values as big as 99,999,999,999, but no more. Trying to sprintf("%011d", $myNum) them outputs an overflow: -2147483648.

I cannot use the BigInt module because in this case I should deeply change the code. I cannot manage the format as string, sprintf("%011s", $numero), because the minus sign is incorrectly handled.

How can I manage this? Could pack/unpack be of some help?

like image 913
Daniel Avatar asked Dec 14 '22 04:12

Daniel


1 Answers

Try formatting it as a float with no fraction part:

$ perl -v
This is perl, v5.6.1 built for sun4-solaris
...

$ perl -e 'printf "%011d\n", 99999999999'
-0000000001

$ perl -e 'printf "%011.0f\n", 99999999999'
99999999999
like image 69
glenn jackman Avatar answered Jan 04 '23 23:01

glenn jackman