Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import decimal
  2.  
  3. a = decimal.Decimal('54.12')
  4. b = decimal.Decimal('54.00')
  5.  
  6. for n in (a, b):
  7. print("You have just bought an item that cost ${0:.{1}f}."
  8. .format(n, 0 if n == n.to_integral() else 2))
  9.  
  10. >>> import decimal
  11. >>> n = decimal.Decimal('54.12')
  12. >>> print('%g' % n)
  13. '54.12'
  14. >>> n = decimal.Decimal('54.00')
  15. >>> print('%g' % n)
  16. '54'
  17.  
  18. >>> dollars = Decimal(repr(54.12))
  19. >>> print "You have just bought an item that cost ${}.".format(dollars)
  20. You have just bought an item that cost $54.12.
  21.  
  22. round = x + 0.5
  23. s = str(round)
  24. dot = s.find('.')
  25. print(s[ : dot])
  26.  
  27. >>> from decimal import Decimal
  28. >>> print Decimal("12.00").normalize()
  29. >>> 12
  30. >>> print Decimal("12.54").normalize()
  31. >>> 12.54
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement