Skip to content Skip to sidebar Skip to footer

Python - Convert Number To Exponential

How can i convert an array like this: [ 76809102.22 38393173.33 -17066.67 -48000000. 0. 0. -28809102.22 -38393173.33 -17066.67] to exponential? [ 7.

Solution 1:

If x is a NumPy array, then you could use

np.set_printoptions(formatter={'float': '{: 0.8e}'.format})

to change the way floats are displayed:

import numpy as np

x = np.array([76809102.22, 38393173.33, -17066.67, -48000000., 0., 0., -28809102.22, 
              -38393173.33, -17066.67])
np.set_printoptions(formatter={'float': '{: 0.8e}'.format})
print(x)

yields

[ 7.68091022e+07  3.83931733e+07 -1.70666700e+04 -4.80000000e+07
  0.00000000e+00  0.00000000e+00 -2.88091022e+07 -3.83931733e+07
 -1.70666700e+04]

Solution 2:

Use the e format for representing a decimal number with exponential notation:

'{:e}'.format(1.0)
'1.000000e+00'

Post a Comment for "Python - Convert Number To Exponential"