最短相同十进制表示法

2024-09-29 19:23:05 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
drop trailing zeros from decimal

如何得到比较相等的Decimal的最短表示?你知道吗

例如:

  • Decimal('89.00')->;'89'
  • Decimal('123.010')->;'123.01'
  • Decimal('0.0')->;'0'

目前,我有自己的实现,它首先转换为字符串

if chanstr.endswith('0'):
    chanstr = chanstr[:chanstr.rfind('.')]

哪一个写得很好,而且只有2个LOC;但是有没有更好的方法来写这个呢?你知道吗


Tags: 字符串fromgtifzeroslocdropdecimal

热门问题