Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Displaying big numbers as decimal

by Anonymous Monk
on Jul 05, 2001 at 15:44 UTC ( [id://94060]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a 20-digit long number that gives -1 when I attempt to printf it as a %d. It defaults to expontential notation when I use %s. Using FAQ entries to conver it to decimal have not worked. Am I missing something? Is there a way to convert my very large number into decimal (or string) form? Furthermore, is there a way, once I have it, to handily insert commas into such a number or string in the appropriate (USA) places? :-) Thanks!

Replies are listed 'Best First'.
Re: Displaying big numbers as decimal
by rob_au (Abbot) on Jul 05, 2001 at 16:08 UTC
    First of all ... have a look at this excellent tutorial by reptile - Definitely worth a ++ ... and read some of the details on integer ranges in Programming Perl or any other good Perl reference
     
    With regard to your problem ... have a look at this code and output ...
     
    #!/usr/bin/perl use strict; my ($value) = 12345678901234567890; select (STDOUT); print "Output using sprint and %d := ", sprintf("%d", $value), "\n"; print "Output using sprint and %D (equivalent to %ld) := ", sprintf("% +D", $value), "\n"; print "Output using sprint and %f := ", sprintf("%f", $value), "\n"; print "Output using sprint and %.0f := ", sprintf("%.0f", $value), "\n +"; exit 0;

     
    And the output from this code ...
     
    Output using sprint and %d := -1 Output using sprint and %D (equivalent to %ld) := -1 Output using sprint and %f := 12345678901234567168.000000 Output using sprint and %.0f := 12345678901234567168

     
    Ooohhh, Rob no beer function well without!
Re: Displaying big numbers as decimal
by kevin_i_orourke (Friar) on Jul 05, 2001 at 16:19 UTC

    If you try %f as the format string to printf you will get the number printed in decimal. You'll probably also see that the last few digits have been replaced by zero, it's too big for Perl to store it accurately.

    $a = 12345678901234567890; printf("%f\n", $a);

    Gives: 12345678901234567000.000000 on my Windows NT system.

    For handling large numbers you could try Math::BigInt or Math::BigFloat (they both came with my ActivePerl distribution). I also recommend PDL if you're doing mathematical stuff with the numbers.

    For formatting the number with commas, etc. I found Number::Format on CPAN.

    I've not tried any of these modules myself (apart from PDL) so they might not do what you want.

    Kevin O'Rourke

Re: Displaying big numbers as decimal
by etj (Deacon) on Jul 08, 2022 at 19:28 UTC
    If you use %s, you asked Perl to stringify the value so it does that, in the way you saw, evidently with e-notation.

    %d tells Perl to convert it to a signed integer; 32-bit signed integers (which may have been the norm in 2001 when this was asked) can only count up to about 2e9, i.e. a 10-digit number, and I believe it will overflow to -1 as you saw. Even a 64-bit signed (%ld) would top out at 8e18, which is a 19-digit decimal number.

    %f (as recommended by another reply) tells Perl to treat the number as floating-point, and hopefully a double-precision one. That has a 53-bit mantissa, which can handle up to about 8e15 (a 16-digit number).

    %Lf would be a long double (which in C99 only means "at least double precision"); on x86 CPUs this is generally an 80-bit number, with a 63-bit mantissa, which can go up to 8e18 (same as a signed 64-bit integer).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://94060]
Approved by hippo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-23 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found