exponent digits in scientific notation in Python
<p>In Python, scientific notation always gives me 2 digits in exponent:</p>

<pre><code>print('%17.8E\n' % 0.0665745511651039)
6.65745512E-02
</code></pre>

<p>However, I badly want to have 3 digits like:</p>

<pre><code>6.65745512E-002
</code></pre>

<p>Can we do this with a built-in configuration/function in Python?</p>

<p>I know my question is basically the same question as: <a href="http://stackoverflow.com/questions/9910972/python-number-of-digits-in-exponent">Python - number of digits in exponent</a>, but this question was asked 4 years ago and I don't want to call such a function thousand times. I hope there should be a better solution now.</p>

<p>You can use your own formatter and override <code>format_field</code>:</p>

<pre><code>import string
class MyFormatter(string.Formatter): 
    def format_field(self, value, format_spec):
        ss = string.Formatter.format_field(self,value,format_spec)
        if format_spec.endswith('E'):
            if ( 'E' in ss):
                mantissa, exp = ss.split('E')
                return mantissa + 'E'+ exp[0] + '0' + exp[1:]                  
        return ss

print( MyFormatter().format('{0:17.8E}',0.00665745511651039) )
</code></pre>

python scientific-notation exponent