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

Format string to alternate between fixed and scientific form, in order to show n digits at most

  • Thread starter Thread starter mins
  • Start date Start date
M

mins

Guest
I've difficulties to master the g format for floats. I'm trying to find a format which shows 2 figures at most. This requires to switch from f to e according to the magnitude.

My constraints are:

  • No change to configuration files
  • No non-significant zeros
  • No extra spaces (I've used width in the example below, but this is just for the question).
  • If possible replace default 1.99e00 by 1.99.

There is g intended for that, but I can't find the correct combination of width and precision. I'm not skilled enough to know if there is a solution.



The first line are numbers to be printed, the second line is what I'm trying to get. The last is my best attempt:

Code:
   0.01234560    0.12345600    1.23456000   12.34560000  123.45600000 1234.56000000
     1.23e-02      1.23e-01          1.23         12.35      1.23e+02      1.23e+03 
        0.012          0.12           1.2            12       1.2e+02       1.2e+03

The code:

Code:
numbers = [1.23456 * 10**k for k in range(-2,4)]
print(*[f'{x:12.8f}' for x in numbers], sep=' ')
# Desired formatting
for x in numbers:
    if x < 1 or x > 99: print(f'{x:12.2e}', end=' ')
    else: print(f'{x:12.2f}', end=' ')
print()
# Best approximation
for x in numbers: print(f'{x:12.2g}', end=' ')
<p>I've difficulties to master the <code>g</code> <a href="https://docs.python.org/2/library/stdtypes.html#string-formatting-operations" rel="nofollow noreferrer">format for floats</a>. I'm trying to find a format which shows 2 figures at most. This requires to switch from <code>f</code> to <code>e</code> according to the magnitude.</p>
<p>My constraints are:</p>
<ul>
<li>No change to configuration files</li>
<li>No non-significant zeros</li>
<li>No extra spaces (I've used width in the example below, but this is just for the question).</li>
<li>If possible replace default <code>1.99e00</code> by <code>1.99</code>.</li>
</ul>
<p>There is <code>g</code> intended for that, but I can't find the correct combination of width and precision. I'm not skilled enough to know if there is a solution.</p>
<hr />
<p>The first line are numbers to be printed, the second line is what I'm trying to get. The last is my best attempt:</p>
<pre><code> 0.01234560 0.12345600 1.23456000 12.34560000 123.45600000 1234.56000000
1.23e-02 1.23e-01 1.23 12.35 1.23e+02 1.23e+03
0.012 0.12 1.2 12 1.2e+02 1.2e+03
</code></pre>
<p>The code:</p>
<pre><code>numbers = [1.23456 * 10**k for k in range(-2,4)]
print(*[f'{x:12.8f}' for x in numbers], sep=' ')
# Desired formatting
for x in numbers:
if x < 1 or x > 99: print(f'{x:12.2e}', end=' ')
else: print(f'{x:12.2f}', end=' ')
print()
# Best approximation
for x in numbers: print(f'{x:12.2g}', end=' ')
</code></pre>
 

Latest posts

Top