Reputation: 4775
Python keeps using fixed format for large floats:
>>> str(1.0e-2)
'0.01'
>>> str(1.0e+2)
'100.0'
>>> str(1.0e-10)
'1e-10'
>>> str(1.0e+10)
'10000000000.0'
How can I make it print 1e+10
in the last case, similar to c++:
std::cout << 1.0e-2 << '\n'
<< 1.0e+2 << '\n'
<< 1.0e-10 << '\n'
<< 1.0e+10 << '\n';
0.01
100
1e-10
1e+10
I don't want to always use scientific notation.
How do I make Python conversion behave similar to C++ general format for
str(x)
when x
is a float?
Upvotes: 2
Views: 71
Reputation: 3611
You can do this with scientific notation
"{:e}".format(1.0e-7)
Or you can choose how many digits to show after the decimal by adding .n
before the e
"{:.2e}".format(1.0e-7)
Upvotes: 3