0

I am trying to write a Python code where the user gives a number and the number of decimals it wants to be rounded to. Then the program returns the first number in scientific notation.

My code:

def csign(a,b):   
   #a=number,b=decimals
   a=float(a)
   b=float(b)
   return "{:.b e}".format(a)

The desired functionality should work like:

input: csign(123456,3)
output: 1.23 e5

But I get an error saying

Format specifier missing precision.

Is there any way I could make the program recognize b as a precision?

Hussain
  • 5,057
  • 6
  • 45
  • 71
L.R.
  • 423
  • 2
  • 5
  • 15

1 Answers1

2

You can nest format specifiers:

return '{:.{}e}'.format(a, b)
user2357112
  • 260,549
  • 28
  • 431
  • 505