简体   繁体   English

如何使用科学记数法打印有限位数

[英]how to print finite number of digits USING the scientific notation

I have some values that I need to print in scientific notation (values of the order of 10^-8, -9) But I would like to don't print a long number, only two digits after the . 我有一些需要用科学计数法打印的值(大约10 ^ -8,-9的值),但是我不想打印长整数,只在数字后两位。

something as: 作为:

9.84e-08

and not 并不是

9.84389879870496809597e-08

How can I do it? 我该怎么做? I tried to use "%.2f" % a where 'a' is the number containing the value but these numbers appear as 0.00 我尝试使用"%.2f" % a ,其中“ a”是包含值的数字,但这些数字显示为0.00

试试这个:

print "%.2e"%9.84389879870496809597e-08 #'9.84e-08'

This works with format function of the string (as % may be soon deprecated) 这适用于字符串的format功能(因为%可能很快就会弃用)

>>> n
9.843898798704968e-08
>>> print ("{0:.2e}".format(n))
9.84e-08

%f stands for Fixed Point and will force the number to show relative to the number 1 (1e-3 is shown as 0.001). %f表示定点,将强制数字显示相对于数字1(1e-3显示为0.001)。 %e stands for Exponential Notation and will give you what you want (1e-3 is shown as 1e-3). %e代表指数表示法,它将为您提供所需的内容(1e-3显示为1e-3)。

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

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