[issue23602] Implement __format__ for Fraction

Wolfgang Maier report at bugs.python.org
Sun Mar 29 22:25:15 CEST 2015


Wolfgang Maier added the comment:

Initially, I also thought that this should be addressable with Fraction.__round__ or an optimized variation of it, but Tuomas is right that it gets complicated by the fact that you need to cope with the different format specifiers and not all of them fit the algorithm.
In particular, scientific notation poses a problem because it may require a lot more precision in the calculation than the one finally used for formatting. Consider:

>>> float(round(Fraction(4, 27000), 6))
0.000148

but
>>> format(4/27000, 'e')
'1.481481e-04'

Trying to deal with this in pure Python quickly slows your code (at least a few naive attempts of mine) unacceptably compared to Tuomas' patch.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23602>
_______________________________________


More information about the Python-bugs-list mailing list