简体   繁体   中英

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 .

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

试试这个:

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

This works with format function of the string (as % may be soon deprecated)

>>> 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). %e stands for Exponential Notation and will give you what you want (1e-3 is shown as 1e-3).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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