ignoring_gravity
ignoring_gravity

Reputation: 10476

Format float with 2 digits before decimal, 6 after decimal

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?

Upvotes: 1

Views: 436

Answers (1)

joanis
joanis

Reputation: 12211

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'

Upvotes: 4

Related Questions