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
  • 27,428
  • 11
  • 44
  • 69
Marcos Dias
  • 63
  • 1
  • 4
  • `pd.to_numeric`? – Quang Hoang Jul 06 '20 at 00:42
  • https://stackoverflow.com/questions/49836620/converting-exponent-or-scientific-number-into-integer-in-pandas-python – wwnde Jul 06 '20 at 00:45
  • 3
    It's not clear what the problem is. A number like n.nnnnne+nnn can be read as an ordinary float -- it is the same kind of number as n.nnnnnn from the point of the input parser. Maybe you can say more about what you are trying to do. – Robert Dodier Jul 06 '20 at 00:46
  • 1
    @QuangHoang It won't help. – Red Jul 06 '20 at 00:48
  • 1
    is the point just to make them look different? cause it won't affect anything you do with them – Derek Eden Jul 06 '20 at 01:13
  • "How can I convert it to a floating point number and remove the the exponent without affecting the value?" This does not make sense. There is **exactly one** floating point number which has the same value (so anything else you "converted it" to would be "affecting the value"). That value is **written with** the `e` that you see, by default. If you want it to be **represented** differently, that is a different question. If you want to convert it to an **integer**, that is also possible. But you cannot "remove the exponent" because that is an integral part of what the number **is**. – Karl Knechtel Dec 05 '22 at 12:00
  • It is as if you had a can of blue paint, and wanted to remove **the word** "blue" from **the paint itself**. You can use other words to **describe** the paint. You can use the paint to paint a wall. You can observe the paint under different lighting conditions. But you cannot make the paint stop fundamentally being "blue paint", without changing its pigment. – Karl Knechtel Dec 05 '22 at 12:01

2 Answers2

5

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
  • 12,573
  • 2
  • 13
  • 23
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
Red
  • 26,798
  • 7
  • 36
  • 58