code_junkie

python limiting floats to two decimal points

Nov 13th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. >>> a
  2. 13.949999999999999
  3.  
  4.  
  5. >>> round(a, 2)
  6. 13.949999999999999
  7.  
  8. >>>125650429603636838/(2**53)
  9. 13.949999999999999
  10.  
  11. >>> 234042163/(2**24)
  12. 13.949999988079071
  13.  
  14. a=13.946
  15. print(a)
  16. 13.946
  17. print("%.2f" % a)
  18. 13.95
  19. round(a,2)
  20. 13.949999999999999
  21. print("%.2f" % round(a,2))
  22. 13.95
  23.  
  24. >>> "%.2f" % 3.14159
  25. '3.14'
  26. >>> "%.2f" % 13.9499999
  27. '13.95'
  28.  
  29. >>> a = 13.95
  30. >>> a
  31. 13.949999999999999
  32. >>> print "%.2f" % a
  33. 13.95
  34.  
  35. >>> 0.1
  36. 0.10000000000000001
  37.  
  38. >>> round(0.1, 1)
  39. 0.10000000000000001
  40.  
  41. >>> sum = 0.0
  42. >>> for i in range(10):
  43. ... sum += 0.1
  44. ...
  45. >>> sum
  46. 0.99999999999999989
  47.  
  48. "{0:.2f}".format(13.949999999999999)
  49.  
  50. a = 13.949999999999999
  51. round(a,2)
  52. print a
Add Comment
Please, Sign In to add comment