Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import decimal
- a = decimal.Decimal('54.12')
- b = decimal.Decimal('54.00')
- for n in (a, b):
- print("You have just bought an item that cost ${0:.{1}f}."
- .format(n, 0 if n == n.to_integral() else 2))
- >>> import decimal
- >>> n = decimal.Decimal('54.12')
- >>> print('%g' % n)
- '54.12'
- >>> n = decimal.Decimal('54.00')
- >>> print('%g' % n)
- '54'
- >>> dollars = Decimal(repr(54.12))
- >>> print "You have just bought an item that cost ${}.".format(dollars)
- You have just bought an item that cost $54.12.
- round = x + 0.5
- s = str(round)
- dot = s.find('.')
- print(s[ : dot])
- >>> from decimal import Decimal
- >>> print Decimal("12.00").normalize()
- >>> 12
- >>> print Decimal("12.54").normalize()
- >>> 12.54
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement