简体   繁体   English

如何在 python 中将 E 表示法字符串转换为具有固定指数的浮点数

[英]How to convert a E notation string to a float with fixed exponent in python

My instrumentation device returns strings of data such as 2.89E-6, 9.87E-1, 4.18E-4 etc.我的仪表设备返回数据字符串,例如 2.89E-6、9.87E-1、4.18E-4 等。

How can I change the representation such that the exponent is always -3.如何更改表示以使指数始终为-3。 My intention is to manually extract the Mantissa of the resultant expression.我的意图是手动提取结果表达式的尾数。

Desired output:所需的 output:

string 2.89E-6 becomes-> float 0.00289E-3字符串2.89E-6 > 浮动0.00289E-3

string 9.87E-1 becomes-> float 987E-3字符串9.87E-1 > 浮动987E-3

string 4.18E-4 becomes-> float 0.418E-3字符串4.18E-4 > 浮动0.418E-3

Eventually, would like to extract the mantissa-> 0.000289, 987, 0.418 respectively as my “final” output.最终,想提取尾数-> 0.000289, 987, 0.418 分别作为我的“最终” output。

You can use the decimal module for manipulation of decimal numbers, then convert to float at the end if needed.您可以使用decimal模块来处理十进制数,然后在需要时转换为float

import decimal

data = ['2.89E-6', '9.87E-1', '4.18E-4']

for s in data:
    n = decimal.Decimal(s)
    print(float(n * 1000))

Output: Output:

0.00289
987.0
0.418

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 用科学记数法固定指数? - Python - fixed exponent in scientific notation? Python 将字符串转换为无科学记数法的浮点数 - Python Convert String to Float without Scientific Notation Python - 如何不在科学记数法中获得指数(例如 1.2**1000 = 1.5179100891722457e+79) - Python - How to not get exponent in scientific notation (e.g 1.2**1000 = 1.5179100891722457e+79) 如何在 Python 中转换指数并去掉“e+”? - How to convert exponent in Python and get rid of the 'e+'? 如何在Python中将浮点表示法转换为10种科学表示法的功效? - How to convert float notation to power of 10 scientific notation in Python? 将字符串(科学计数法)转换为浮点数 - Convert string (in scientific notation) to float Python - 将科学记数法字符串转换为保留小数位的浮点数 - Python - Convert scientific notation string to float retaining decimal places 在 Python 中使用工程符号(带有 SI 前缀)将浮点数转换为字符串 - Convert float number to string with engineering notation (with SI prefix) in Python 在Python中解析表示带*浮点数的字符串 - Parsing a string representing a float *with an exponent* in Python 如何在python中将float转换为定点小数 - How to convert float to fixed point decimal in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM