2

I want to format a number so that the mantissa is between 0 and 1 instead of 1 and 10 in scientific notation.

For example;

a=7.365
print("{:.6e}".format(a))

This will output 7.365000e+00, but I want it to be 0.736500e+01.

6
  • 5
    Actually, I suspect you want it to be 0.736500e+01
    – lastchance
    Commented Jul 5 at 11:11
  • 6
    I hope you are aware that Python 2 is out of support since several years back. You should probably ignore Python 2, and spend your time on the currently recommended and supported version of the language, which is Python 3.
    – tripleee
    Commented Jul 5 at 11:12
  • Should 1.000000e... be included or excluded? Commented Jul 5 at 11:15
  • I understand "between 0 and 1 instead of 1 and 10" as [0,1) instead of [1,10). Commented Jul 5 at 11:18
  • @WalterTross A value of 1 would 0.1e+01, so 1 is excluded Commented Jul 5 at 11:19

2 Answers 2

1

I suppose you could always try constructing your own preformatted string. (No idea if this works in Python 2.7, though).

import math

def fixit( x, n ):
   if x == 0.0: return f" {x:.{n}e}"

   s = ' ' if x >= 0 else '-'
   y = math.log10( abs(x) )
   m = math.floor(y) + 1
   z = 10 ** ( y - m )
   return s + f"{z:.{n}f}e{m:+03d}"

for x in [ -7635, -763.5, -76.35, -7.635, -0.7635, -0.07635, -0.007635, 0.007635, 0.07635, 0.7635, 7.635, 76.35, 763.5, 7635 ]:
    print( fixit( x, 6 ) )
for x in [ -10, -1, -0.1, -0.01, 0.0, 0.01, 0.1, 1, 10 ]:
    print( fixit( x, 6 ) )

Output:

-0.763500e+04
-0.763500e+03
-0.763500e+02
-0.763500e+01
-0.763500e+00
-0.763500e-01
-0.763500e-02
 0.763500e-02
 0.763500e-01
 0.763500e+00
 0.763500e+01
 0.763500e+02
 0.763500e+03
 0.763500e+04
-0.100000e+02
-0.100000e+01
-0.100000e+00
-0.100000e-01
 0.000000e+00
 0.100000e-01
 0.100000e+00
 0.100000e+01
 0.100000e+02
5
  • f-strings aren't available in Python 2.7, so the f"..." style will not work. Commented Jul 5 at 14:32
  • 1
    Ah well. Might be easier just to return the mantissa and exponent separately and let the OP write them out in f and d fields as he/she wants. Dealing with the signs and the simple case x=0 were more irritating.
    – lastchance
    Commented Jul 5 at 14:37
  • 1
    You can switch if x == 0.0: return f" {x:.{n}e}" to if x == 0.0: return " {0:.{1}e}".format(x, n) and return s + f"{z:.{n}f}e{m:+03d}" to return s + "{0:.{1}f}e{2:+03d}".format(z, n,int(m)) and it will be Python 2.7 compatible. Commented Jul 5 at 14:52
  • Thanks for the comment, @Matt Pitkin. I haven't programmed in Python long enough to have ever used Python 2.7. Not much motivation to do so now. Also, I'm much more used to Fortran, where controlling how many significant digits go in front of the decimal point is straightforward in the format statement.
    – lastchance
    Commented Jul 5 at 15:06
  • An elegant solution that works - thanks @lastchance Commented Jul 5 at 15:07
1

You could do something like the following:

import math

x = 0.07365

def zero_one_mant(x):
    """
    Return a string for the input number `x`, where the mantissa is always
    between 0 and 1 and the exponent is adjusted accordingly.
    """

    exp = int(math.floor(math.log10(x))) + 1
    
    mant = x / 10**exp
    
    return "{0:.6f}e{1:+03d}".format(mant, exp)
    

for x in [0.07365, 7.365, 73.65]:
    print(zero_one_mant(x))

0.736500e-01
0.736500e01
0.736500e02

Not the answer you're looking for? Browse other questions tagged or ask your own question.