简体   繁体   English

如何在 Python 中摆脱科学记数法并转换为浮点数?

[英]How do I get rid of scientific notation and covert to a floating number in Python?

I have been having trouble with Python in that I can't stop scientific notation and convert to a floating number.我在使用 Python 时遇到了麻烦,因为我无法停止科学记数法并转换为浮点数。 I have tried multiple methods, and none of them have done what I have wanted.我尝试了多种方法,但都没有达到我想要的效果。 Below is the code for a random number generator that continues on until it finds the number 7. At the end, it gives you statistics on the run.下面是一个随机数生成器的代码,它会继续运行,直到找到数字 7。最后,它会为您提供运行的统计信息。 One gets added to x every time the loop goes through Thank you in advance.每次循环通过时都会将一个添加到 x 提前谢谢。 Edit: The output is usually something like this:编辑:输出通常是这样的:

Your computer just went through 279754 integers to find 7!您的计算机刚刚通过 279754 个整数找到了 7!

Your computer just took 4.462186096363586e-07 seconds to run!您的计算机只用了 4.462186096363586e-07 秒即可运行!

Your computer took 1.5950392474686997e-12 seconds per integer!您的计算机每个整数需要 1.5950392474686997e-12 秒!

What I have is:我所拥有的是:

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!")

Use format and specify length after floating point.使用格式并在浮点后指定长度。

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))

Your computer just went through 371 integers to find 7!您的计算机刚刚通过 371 个整数找到了 7!
Your computer just took 0.026945 seconds to run!您的计算机只需要 0.026945 秒即可运行!
Your computer took 0.00007263 seconds per integer!您的计算机每个整数需要 0.00007263 秒!

Use .format() instead.改用.format() To not get the scientific notation (the e-number part), enter the specified length of the number.要不获取科学记数法( e-number部分),请输入数字的指定长度。

import time

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

The .20 part means that the float will be 20 numbers after the decimal point. .20部分意味着浮点数将是小数点后的 20 个数字。 Change the number to change the length, such as .50 will have the float have 50 numbers after the decimal point and so on.更改数字以更改长度,例如.50将使浮点数小数点后有 50 个数字等等。

Computers run rather fast so the time can be surprisingly low at times.计算机运行速度相当快,因此有时时间会出奇地低。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM