I have multiple instances Decimalin Python. I want to format them so that
Decimal('1') => '1.00'
Decimal('12.0') => '12.00'
Decimal('314.1') => '314.10'
Decimal('314.151') => '314.151'
therefore, ensuring that there are always at least two decimal places, possibly more. Although there is no shortage of solutions for rounding to ndecimal places, I cannot find clear ways to provide a lower bound on the number.
My current solution is to calculate:
first = '{}'.format(d)
second = '{:.2f}'.format(d)
and take which one is longer. However, this seems somewhat hacky.
source
share