Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove leading 0 in exponential format in python

I have the following code in Python 3.1

"{0:.6E}".format(9.0387681E-8)

Which gives a string of 9.038768E-08, but I want the string 9.038768E-8 with the leading 0 of E-08 removed. How should I go about this?

like image 231
Michael Avatar asked May 29 '26 13:05

Michael


1 Answers

you could do something like this

"{0:.6E}".format(9.0387681E-8).replace("E-0", "E-")

or better as JBernardo suggests

format(9.0387681E-8, '.6E').replace("E-0", "E-")

You need to do a replace for E+0 as well if you have big numbers

like image 187
John La Rooy Avatar answered May 31 '26 05:05

John La Rooy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!