from decimal import Decimal def clean(d): if d == d.to_integral(): return d.quantize(Decimal(1)) else: return d.quantize(Decimal('1.000')).normalize()