Search code examples
pythonfloating-pointstring-formatting

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?


Solution

  • 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