# How to convert a floating point number to a 64 bit integer?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I need to convert a floating point number to a 64 bit integer (long
long). How to do that in perl? Thanks.

## Re: How to convert a floating point number to a 64 bit integer?

You will need to specify a little more about where these numbers are
coming from. Probably you want perldoc -f pack.

Ben

## Re: How to convert a floating point number to a 64 bit integer?

The number comes from calculation. For example:

my \$a = 2.0**37;

Here I cannot use 2**37 directly since the result is -1.

I have found a way to "convert" it although I think there should be a
better way:

my \$b = sprintf ("%.0f", \$a);

## Re: How to convert a floating point number to a 64 bit integer?

use bigint;
\$a = 2.0**37;
print int(\$a),"\n";

137438953472

## Re: How to convert a floating point number to a 64 bit integer?

That was my interpretation of the question, too. But it seems I was
mistaken.

The result of this is a string, not a 64 bit integer.

And the result of this is a bigint, which is also not the same as a 64
bit integer.

Perl can also be compiled to use 64 bit integers on most platforms.

But the real question is: What is the 64 bit integer for? Until
this is known, there is no way to tell whether a real 64 bit int is
needed or  string or a bigint (or
maybe even a float) can be used instead.

hp

## Re: How to convert a floating point number to a 64 bit integer?

Sorry, I did not make it clear. Actually I just need a string which is
the result of 2.0**37. So the simplest solution, as Ben said, is

perl -le'print 2.0**37'

That solved my problem. Thanks.

## Re: How to convert a floating point number to a 64 bit integer?

I don't know what you mean by this. Even on a perl with 32bit IVs, I get

~% perl -le'print 2.0**37'
137438953472

since perl will auto-upgrade to a NV if necessary. (IV is perl's
internal integer type, NV is its internal floating type. Usually IV is
your compiler's int and NV is its double, though you can Configure perl
to use different types if desired.) Most machines have at least 53bit
doubles nowadays, so they can hold numbers up to 2^53 with integer
precision.

This converts it into a string. "\$a" will do the same thing, if you
really need to. Nearly all the time you can just ignore the
integer/float/string distinction, and let perl handle it.

You need to tell us what you are actually doing. Post a *short*,
*complete* example program that doesn't do what you expect, and tell us
both what it does and what you wanted it to do.

You might want to read the Posting Guidelines that are posted here
fairly regularly.

Ben

## Re: How to convert a floating point number to a 64 bit integer?

use bignum;

will make it work right by using long integers.

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities