vendredi 8 janvier 2021

Python f-string surprising results on floats

I am trying to format float numbers in a fixed point notation: x.xxx, three digits following the decimal point regardless of the value of the number. I am getting surprising results. The first in particular would suggest that it is giving me three significant places rather than three digits after the decimal point. How do I tell it what I really want?

>>> print(f"{.0987:5.03}")  
0.0987
*expected: 0.099*
>>> print(f"{0.0:05.03}") 
000.0
*expected: 0.000*
>>> print(f"{0.0:5.3}")   
  0.0

Aucun commentaire:

Enregistrer un commentaire