python - Limiter les flotteurs à deux points décimaux

Mots clés : pythonfloating-pointroundingprecision

meilleur 5 Réponses python - Limiter les flotteurs à deux points décimaux

vote vote

97

>>> 125650429603636838/(2**53) 13.949999999999999  >>> 234042163/(2**24) 13.949999988079071  >>> a = 13.946 >>> print(a) 13.946 >>> print("%.2f" % a) 13.95 >>> round(a,2) 13.949999999999999 >>> print("%.2f" % round(a, 2)) 13.95 >>> print("{:.2f}".format(a)) 13.95 >>> print("{:.2f}".format(round(a, 2))) 13.95 >>> print("{:.15f}".format(round(a, 2))) 13.949999999999999 
vote vote

80

"{:.2f}".format(13.949999999999999) 
float("{:.2f}".format(13.949999999999999)) 
>>> x = 13.949999999999999999 >>> x 13.95 >>> g = float("{:.2f}".format(x)) >>> g 13.95 >>> x == g True >>> h = round(x, 2) >>> h 13.95 >>> x == h True 
vote vote

74

>>> round(14.22222223, 2) 14.22 
vote vote

67

a = 13.949999999999999 format(a, '.2f')  13.95 
vote vote

57

>>> f'{a:.2f}' 
>>> print(f'Completed in {time.time() - start:.2f}s') 

Questions similaires