3

I'm starting with Python and I recently came across a dataset with big values. One of my fields has a list of values that looks like this: 1.3212724310201994e+18 (note the e+18 by the end of the number).

How can I convert it to a floating point number and remove the the exponent without affecting the value?

Emma
  • 26,487
  • 10
  • 35
  • 65
Marcos Dias
  • 63
  • 1
  • 4

2 Answers2

3

First of all, the number is already a floating point number, and you do not need to change this. The only issue is that you want to have more control over how it is converted to a string for output purposes.

By default, floating point numbers above a certain size are converted to strings using exponential notation (with "e" representing "*10^"). However, if you want to convert it to a string without exponential notation, you can use the f format specifier, for example:

a = 1.3212724310201994e+18

print("{:f}".format(a))

gives:

1321272431020199424.000000

or using "f-strings" in Python 3:

print(f"{a:f}")

here the first f tells it to use an f-string and the :f is the floating point format specifier.

You can also specify the number of decimal places that should be displayed, for example:

>>> print(f"{a:.2f}")   # 2 decimal places
1321272431020199424.00

>>> print(f"{a:.0f}")   # no decimal places
1321272431020199424

Note that the internal representation of a floating-point number in Python uses 53 binary digits of accuracy (approximately one part in 10^16), so in this case, the value of your number of magnitude approximately 10^18 is not stored with accuracy down to the nearest integer, let alone any decimal places. However, the above gives the general principle of how you control the formatting used for string conversion.

alani
  • 11,960
  • 2
  • 10
  • 22
  • Do you know why there's a trailing `24` instead of `00` in both of our answers? – Ann Zen Jul 06 '20 at 01:02
  • @AnnZen No doubt because there are not enough bits of precision in a float to distinguish between these. – alani Jul 06 '20 at 01:04
  • Yeah, I just thought it was strange that instead of random numbers, it's `24`. – Ann Zen Jul 06 '20 at 01:05
  • 1
    @AnnZen That will presumably be (part of) the decimal representation of whatever exact binary value is stored using the available bits. – alani Jul 06 '20 at 01:06
  • @AnnZen I see that `1 << sys.float_info.mant_dig` is approximately 9e15, compare with 1.3e18... By the way, if you don't mind me saying, I do think that this question is more about the OP's general understanding of how floats work in Python, rather than the need for arbitrary-precision numbers in this case (which is what `Decimal` provides). Exactly as you are seeing, by the time that the number has been stored as a float, it is too late to usefully convert to `Decimal` because rounding (in binary) will _already_ have happened. – alani Jul 06 '20 at 01:17
  • @AnnZen for every decimal floating point number, there's one binary `float` which is closest, but it's rare for that `float` and the number to be exactly equal. But for every binary `float` there is a decimal that is exactly equal, but it may take more digits than you would expect to express it completely. A good converter will express it consistently and accurately, and evidently both `Decimal` and the print formatter have good converters. – Mark Ransom Jul 07 '20 at 04:26
2

You can use Decimal from the decimal module for each element of your data:

from decimal import Decimal

s = 1.3212724310201994e+18

print(Decimal(s))

Output:

1321272431020199424
Ann Zen
  • 25,080
  • 7
  • 31
  • 51