这样做可能是更好的方法,但是您可以使用它
.rstrip('0').rstrip('.')来获得所需的结果。以您的数字为例:
>>> s = str(Decimal('2.5') * 10)>>> print s.rstrip('0').rstrip('.') if '.' in s else s25>>> s = str(Decimal('2.5678') * 1000)>>> print s.rstrip('0').rstrip('.') if '.' in s else s2567.8这是gerrit在注释中指出的问题的解决方法:
>>> s = str(Decimal('1500'))>>> print s.rstrip('0').rstrip('.') if '.' in s else s1500


