I have some Decimal
instances in Python. I wish to format them such that
Decimal('1') => '1.00'
Decimal('12.0') => '12.00'
Decimal('314.1') => '314.10'
Decimal('314.151') => '314.151'
hence ensuring that there are always at least two decimal places, possibly more. While there are no shortage of solutions for rounding to n
decimal places I can find no neat ways of ensuring a lower bound on the number.
My current solution is to compute:
first = '{}'.format(d)
second = '{:.2f}'.format(d)
and take which ever of the two is longer. However it seems somewhat hackish.