首页 > 解决方案 > 从科学记数法中提取指数

问题描述

我有一堆科学计数法的数字,我想找到它们的指数。例如:

>>> find_exp(3.7e-13)
13

>>> find_exp(-7.2e-11)
11

因此,我只需要他们的指数,而忽略包括符号在内的所有其他内容。

我在 python 中寻找过这种方式,但类似的问题仅用于格式化目的。

标签: pythonscientific-notation

解决方案


常用对数是您在这里需要的,您可以使用log10+ floor

from math import log10, floor

def find_exp(number) -> int:
    base10 = log10(abs(number))
    return abs(floor(base10))

find_exp(3.7e-13)
# 13

find_exp(-7.2e-11)
# 11

推荐阅读