Mark Dickinson added the comment: Here's a first attempt at Decimal.__format__; the patch is against the trunk, and should be forward ported as usual to 3.0, with obvious minor changes related to str/unicode. It still needs some cleanup and some more tests, but I'm posting it now in the hope of getting some feedback.
I'm adding Raymond Hettinger and Eric Smith to the nosy list in case they want to comment. Some points of interest: * I decided to make plain 'e', 'f' and 'g' formats (without an explicit precision) do no rounding: they return a string that captures the exact value of the Decimal instance (though it can lose information about significant zeros). So format(x, 'e') is basically a way to say 'give me str(x), but always include an exponent', and format(x, 'f') gives a way to print the value without ever including an exponent. format(x, 'g') is identical to str(x), except that it always uses the character 'e' for an exponent (instead of using 'e' or 'E' depending on the context). There are other possible options here (have a default precision; use the precision from the context), but this one seemed to make most sense. I'd appreciate opinions. * the integer format specifiers are not supported. After noticing that the integer format specifiers aren't supported for floats either, I think this is the right thing to do. * the 'n' format specifier is not supported either; it's supposed to use the current locale. I can't find any easy way to do this---it seems as though the only real option is to manually fix the decimal point character, figure out where to place thousands separators, etc. This would add quite a lot of not-really-Decimal-related code to decimal.py, and I'm reluctant to do that. The right solution probably involves writing some support code and putting it somewhere else in the std. lib. * on the subject of not-really-Decimal-related code, it would be great if the parse_format_specifier and format_align functions (near the end of decimal.py) could be moved somewhere else. I'm planning to reuse these functions for Fraction.__format__. ---------- keywords: +patch nosy: +eric.smith, rhettinger versions: +Python 2.6 Added file: http://bugs.python.org/file9539/decimal_format.patch __________________________________ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue2110> __________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com