Home > database >  Format float with 2 digits before decimal, 6 after decimal
Format float with 2 digits before decimal, 6 after decimal

Time:10-11

E.g. if I have 1.234 I'd like to format it as '01.234000.

I can get:

>>> f'{1.234:.6f}'
'1.234000'

but I also need a leading digit - how can I specify that within the f-string?

CodePudding user response:

The formatting specification takes a width before the period, and a zero in front of that to say you want leading zeros instead of leading spaces:

>>> f'{1.234:09.6f}'
'01.234000'
  • Related