spec3
spec3

Reputation: 501

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

Upvotes: 0

Views: 128

Answers (3)

Hjulle
Hjulle

Reputation: 2615

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

Upvotes: 0

Bhargav Rao
Bhargav Rao

Reputation: 52081

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

Upvotes: 2

markcial
markcial

Reputation: 9323

try with this :

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

Upvotes: 2

Related Questions