Jun-26-2020, 08:54 PM
I don't know a way to do this directly with format specifiers. They provide max length, not min. So if the '5.0' is printing out already, I don't think it can be trimmed without trimming all.
If you don't want to deal with special cases, you could pass the string into Decimal and then normalize it.
If you don't want to deal with special cases, you could pass the string into Decimal and then normalize it.
>>> import decimal >>> print(decimal.Decimal('3.24').normalize()) 3.24 >>> print(decimal.Decimal('5.00').normalize()) 5A cheesier way is just to remove any zeros on the right, then remove any decimals on the right.
>>> '3.24'.rstrip('0').rstrip('.') '3.24' >>> '5.00'.rstrip('0').rstrip('.') '5'But if you do it that way, you can't pass it strings without the decimal or you'll have problems.
>>> '500'.rstrip('0').rstrip('.') '5'