CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Want to print long number as number, not scientific notation

 



bulrush
User

Dec 15, 2015, 4:42 AM

Post #1 of 4 (1003 views)
Want to print long number as number, not scientific notation Can't Post

Perl 5.20.1 on Redhat Enterprise Linux

I have a long number: 1336070006978410 that is the key to a hash and it's passed to a subroutine that looks for the initial key. If it doesn't find it, I try to remove leading zeroes by adding zero, at which point my number displays in the debugger as scientific notation.

I've tried printf("%d",$hhid) and printf("%u",$hhid) and both times, in the debugger, I get the output in scientific notation. I just want to remove leading zeroes and print out the number in the debugger as a regular unsigned number.

What am I missing here?

The code I'm using is this:


Code
# Call: ($j1,$j2)=getsalut($hhid,sprintf("%.10g",$hhid+0),$donorid); 
sub getsalut
{my($hhid,$zhhid,$donorid)=@_;
my(@a,@b,$i,$id,$j,$procname,$s,$t);
my($found);

$procname="getsalut";

$found=0; # 1=found
@a=();
if ($hhid)
{
push(@a,$hhid);
}
if ($zhhid)
{
push(@a,$zhhid);
}
if ($donorid)
{
push(@a,$donorid);
}

for ($i=0; $i<=$#a; $i++)
{
$id=$a[$i];
if (exists $salut{$id})
{
@a=split($SEP,$salut{$id});
return($a[0],$a[1]);
}
} # while

if ($found==0)
{
$s="$procname ERROR: hhids ".join(', ',@a).
" not found in \%salut";
print "$s\n";
$misssalut++;
}

return ('',''); # getsalut
}


I guess my second question is, how do I remove leading zeroes without changing the number to scientific notation?

Thank you.
-----


(This post was edited by bulrush on Dec 15, 2015, 5:00 AM)


bulrush
User

Dec 15, 2015, 5:06 AM

Post #2 of 4 (997 views)
Re: [bulrush] Want to print long number as number, not scientific notation [In reply to] Can't Post

Got it!


Code
my $hhid=00033471380008878280; 
my $zhhid=$hhid+0; # Converts to scientific notation.
printf("%.20g",$zhhid)."\n";


I just had to make sure the digits in printf could handle the digits in the actual number.
-----


BillKSmith
Veteran

Dec 15, 2015, 6:24 AM

Post #3 of 4 (993 views)
Re: [bulrush] Want to print long number as number, not scientific notation [In reply to] Can't Post

Your new code does not compile. The right hand side of line 1 is neither a valid perl number nor a string. This suggests that although you may have worked around your problem, you still do not understand it.

If the input to your original subroutine can have leading zeros, it is not a "number" at all, but rather a string of digits. You could remove the leading zeros with a regular expression ($digits =~ /^0*//;). When you add zero, perl first has to convert the string to a number. When you convert back to a string with print or debug, perl displays it in a valid format. (If that number cannot be represented exactly if floating point, it is impossible to get back the original digits.)

I do not know what if any conversion perl does on hash keys. It may be necessary to do exactly the same thing that was done when the hash was created. It probably is not a good idea to use large numbers as hash keys.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Dec 15, 2015, 10:30 AM

Post #4 of 4 (989 views)
Re: [BillKSmith] Want to print long number as number, not scientific notation [In reply to] Can't Post

Hash keys get stringified anyway. So the "large number" should really be handled as a string of characters that happen to be digits.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives