OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How `G` type of python % string formatting behave?

  • Thread starter Thread starter Mahesha999
  • Start date Start date
M

Mahesha999

Guest
I read:

The g and G conversion types choose between floating point or exponential output, depending on the magnitude of the exponent and the value specified for .<precision>. Output is the same as e/E if the exponent is less than -4 or not less than .<precision>. Otherwise, it’s the same as f/F:

Also, I didnt get "if the exponent is less than -4 or not less than .<precision>". Where do we specify exponent explicitly? By "exponent", does it mean number of digits after decimal point? I feel so, as when I tried to have four digits after decimal points, it printed it as f, but when I tried having five digits after decimal points, it printed it as e:

Code:
>>> '%g' % 0.0003
'0.0003'
>>> '%g' % 0.00003
'3e-05'

Q1. Am I correct with above understanding?

Q2. But, then, I didnt get why it did not do same in below example:

Code:
>>> '%g' % 3.14
'3.14'
>>> '%g' % 3.014
'3.014'
>>> '%g' % 3.0014
'3.0014'
>>> '%g' % 3.00014
'3.00014'
>>> '%g' % 3.000014
'3.00001'
>>> '%g' % 3.0000014
'3'

Q3. Finally, I didnt get "or not less than .<precision>" part of "if the exponent is less than -4 or not less than .<precision>". I tried to explicitly specify precision less than and more than 4 digits after decimal point. But it still printed the same:

Code:
>>> '%g' % 0.00003
'3e-05'
>>> '%.3g' % 0.00003
'3e-05'
>>> '%.9g' % 0.00003
'3e-05'
<p>I <a href="https://realpython.com/python-input-output/#the-string-modulo-operator" rel="noreferrer">read</a>:</p>
<blockquote>
<p>The <code>g</code> and <code>G</code> conversion types choose between floating point or exponential output, depending on the magnitude of the exponent and the value specified for <code>.<precision></code>. Output is the same as <code>e</code>/<code>E</code> if the exponent is less than -4 or not less than <code>.<precision></code>. Otherwise, it’s the same as <code>f</code>/<code>F</code>:</p>
</blockquote>
<p>Also, I didnt get "if the exponent is less than -4 or not less than <code>.<precision></code>". Where do we specify exponent explicitly? By "exponent", does it mean number of digits after decimal point? I feel so, as when I tried to have four digits after decimal points, it printed it as <code>f</code>, but when I tried having five digits after decimal points, it printed it as <code>e</code>:</p>
<pre><code>>>> '%g' % 0.0003
'0.0003'
>>> '%g' % 0.00003
'3e-05'
</code></pre>
<p><strong>Q1.</strong> Am I correct with above understanding?</p>
<p><strong>Q2.</strong> But, then, I didnt get why it did not do same in below example:</p>
<pre><code>>>> '%g' % 3.14
'3.14'
>>> '%g' % 3.014
'3.014'
>>> '%g' % 3.0014
'3.0014'
>>> '%g' % 3.00014
'3.00014'
>>> '%g' % 3.000014
'3.00001'
>>> '%g' % 3.0000014
'3'
</code></pre>
<p><strong>Q3.</strong> Finally, I didnt get "or not less than <code>.<precision></code>" part of "if the exponent is less than -4 or not less than <code>.<precision></code>". I tried to explicitly specify precision less than and more than 4 digits after decimal point. But it still printed the same:</p>
<pre><code>>>> '%g' % 0.00003
'3e-05'
>>> '%.3g' % 0.00003
'3e-05'
>>> '%.9g' % 0.00003
'3e-05'
</code></pre>
 
Top