-1

is there a way to print 6.5646961e-07 in this format 656e-09?

I have tried this:

x = 6.5646961e-07
print(f"{lamda_balmer:.2e}")

which gives 6.56e-07

but I would like to have 656e-09

Is there a way to do this? I couldn't find anything on the internet. Thanks in advance.

shafee
  • 15,566
  • 3
  • 19
  • 47

1 Answers1

0

As said in the comments By convention the "e" format only has one digit to the left of the decimal place, so it seems that there is no way around to write an own method printing the float in any other special "e" format.

Try:

x = 656.46961e-09
def print_with_shifted_exp(x, ffrmt='5.2f', efrmt='03d', shift=-2):
   """ x     -> float number
       ffrmt -> required fixed point format
       efrmt -> required exponent format
       shift -> changes exponent to exponent+shift
   """
   str_exp = f'{x:e}'.split('e')[-1]
   int_exp = int(str_exp)
   x = x*10**(-shift-int_exp)
   s = f'{x:{ffrmt}}e{int_exp+shift:{efrmt}}'
   print(s)

print_with_shifted_exp(x, '3.0f', '03d', -2)
print('---')
print_with_shifted_exp(65646.0)
print_with_shifted_exp(6.5646)
print_with_shifted_exp(0.065646)
print_with_shifted_exp(0.00065646)

which prints:

656e-09
---
656.46e002
656.46e-02
656.46e-04
656.46e-06
Claudio
  • 7,474
  • 3
  • 18
  • 48