11 Mar
2014
11 Mar
'14
5:44 a.m.
During recent discussions I came across something unfortunate with formatting Decimals. Formatting with .format works as I would want:
from decimal import Decimal as D '{:1.0e}'.format(D('1.123e+1000')) '1e+1000' '{:1.0e}'.format(D('1.123e-1000')) '1e-1000' '{:.50f}'.format(D('1.1')) '1.10000000000000000000000000000000000000000000000000'
But %-formatting coerces to float:
'%1.0e' % D('1.123e-1000') '0e+00' '%1.0e' % D('1.123e+1000') 'inf' '%.50f' % D('1.1') '1.10000000000000008881784197001252323389053344726562'
Am I doing this wrong? Is this just a limitation of the old-style % formatting or something that could possibly be improved? Oscar