Skip to main content
 首页 » 编程设计

python之如何摆脱科学记数法并在 Python 中转换为 float

2025年05月04日33Renyi-Fan

我一直在使用 Python 时遇到问题,因为我无法停止科学记数法并转换为 float 。我尝试了多种方法,但都没有达到我想要的效果。下面是一个随机数生成器的代码,它会继续运行直到找到数字 7。最后,它会为您提供有关运行的统计信息。每次循环通过时都会将一个添加到 x 在此先感谢您。编辑:输出通常是这样的:

您的计算机刚刚通过 279754 个整数找到了 7!

您的计算机刚刚运行了 4.462186096363586e-07 秒!

您的计算机每个整数花费 1.5950392474686997e-12 秒!

我有的是:

x = 0 
c = time.clock() 
print("Your computer just went through " + str(x) + " integers to find 7!") 
print("Your computer just took " + str(c) + " seconds to run!") 
print("Your computer took " + str(c / x) + " seconds per integer!") 

请您参考如下方法:

在 float 之后使用格式并指定长度。

print("Your computer just went through {} integers to find 7!".format(x)) 
print("Your computer just took {} seconds to run!".format(c)) 
print("Your computer took {0:.8f} seconds per integer!".format(c / x)) 

您的计算机刚刚通过 371 个整数找到了 7!
您的计算机只用了 0.026945 秒来运行!
您的计算机每个整数花费 0.00007263 秒!