python - Fixed digits after decimal with f-strings -
is there easy way python f-strings (pep 498) fix number of digits after decimal point? (specifically f-strings, not other string formatting options .format or %)
for example, want display 2 digits after decimal place. how do that?
a = 10.1234 f'{a:.2}' out[2]: '1e+01' f'{a:.4}' out[3]: '10.12' = 100.1234 f'{a:.4}' out[5]: '100.1'
as can see "precision" has changed meaning "number of places after decimal point" case when using % formatting, total digits. how can always 2 digits after decimal, no matter how big number have?
include type specifier in format expression:
>>> f'{a:.2f}' '10.12'
Comments
Post a Comment