[英]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.