views:

134

answers:

1

Adding 15 digit numbers like 999999999999990 in Perl produces results with a period such as 1.9999999999999e+. When using substr it still produces 1.99999999999, and when using BigInt the result still has a period. What is the correct Perl syntax for Perl 5.8.7 to get the result without the period?

use BigInt;
$acct_hash = substr(($acct_hash + $data[1]),0,15);

BigInt.pm -> /opt/perl5.8.7/lib/5.8.7/Math/BigInt.pm
BigInt -> /opt/perl5.8.7/lib/5.8.7/Math/BigInt.pm
+5  A: 

Use the bigint pragma to get transparent use of Math::BigInt:

#!/usr/bin/perl

use strict;
use warnings;


print 999999999999990 + 999999999999990, "\n";

use bigint;

print 999999999999990 + 999999999999990, "\n";
Chas. Owens
Hi, these are the errors:Variable "$acct_hash" is not imported at s.pl line 416.Variable "$acct_hash" is not imported at s.pl line 416.Variable "@data" is not imported at s.pl line 416.Global symbol "$acct_hash" requires explicit package name at s.pl line 416.Global symbol "$acct_hash" requires explicit package name at s.pl line 416.Global symbol "@data" requires explicit package name at s.pl line 416.BEGIN not safe after errors--compilation aborted at s.pl line 417.Thank you Chas
That is the strict pragma telling you are doing bad things. You can remove the strict pragma to let the script run, but the errors will still be in your code. You should declare your variables before using them with the my function: http://perldoc.perl.org/functions/my.html
Chas. Owens
You keep asking this question and I can't figure out how to answer you because I have no idea why substr is relevant here.
Sinan Ünür
How do I init. the $data[1] variable for the array? use BigInt;sub extract_bank_detail{ $errrec = 0; $extracted = 0; my $acct_hash = 0; my $data = 0; ## don't work. $vol_hash = 0; while (@data = $bill_rec->fetchrow_array())I am almost there, thank you!
Hi Sinan, I have to display no more than 15 digits.
arrays can be initialized like this my @array = (0,1,2,3,4); this will create an array with five elements. But it looks like you are assigning the results of a fetchrow_array call to @data, so any value you set will be overwritten. It also looks like you are using a bunch of global variables. This is an incredibly bad practice. Variables should be passed to functions, not grabbed from the global scope. You may want to post the entire script to [email protected] (subscribe here http://lists.cpan.org/showlist.cgi?name=beginners); it looks like you need lots of help.
Chas. Owens
Thank you Chas. I not good at this. It was found out that adding zeroes resolves the problem.