I have a Dollar price as a Decimal
with a precision of .01
(to the cent.)
I want to display it in string formatting, like having a message "You have just bought an item that cost $54.12."
The thing is, if the price happens to be round, I want to just show it without the cents, like $54
.
How can I accomplish this in Python? Note that I'm using Python 2.7, so I'd be happy to use new-style rather than old-style string formatting.
>>> import decimal
>>> n = decimal.Decimal('54.12')
>>> print('%g' % n)
'54.12'
>>> n = decimal.Decimal('54.00')
>>> print('%g' % n)
'54'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With