This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: decimal.Decimal: __format__ behaviour
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: MonsieurPaul, mark.dickinson, paul.moore, skrah
Priority: normal Keywords:

Created on 2015-03-27 13:01 by MonsieurPaul, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg239394 - (view) Author: Paul Eckhardt (MonsieurPaul) Date: 2015-03-27 13:01
The bahaviour of __format__ for decimal.Decimal type differs in one detail from float:
The minimum number of digits used for the exponent is 1 for Decimal, and 2 for float.
e.g.: "{:8.2E}".format(1.0) --> "1.00E+00"
      "{:8.2E}".format(Decimal(1.0)) --> " 1.00E+0"

Is there any reason for this?
If not, the following patch should do:

--- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py	2015-03-27 12:55:22.000000000 +0100
+++ ./decimal.py	2015-03-27 14:00:09.000000000 +0100
@@ -6116,7 +6116,7 @@
 
     if exp != 0 or spec['type'] in 'eE':
         echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']]
-        fracpart += "{0}{1:+}".format(echar, exp)
+        fracpart += "{0}{1:+03d}".format(echar, exp)
     if spec['type'] == '%':
         fracpart += '%'
msg239404 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2015-03-27 14:21
I believe it's the way it is because Python follows the Standard Decimal Specification, which includes tests for conformance to the current formatting behaviour.
msg239430 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-03-27 19:34
> I believe it's the way it is because Python follows the Standard Decimal Specification

Yes, exactly.  This is behaving as designed and as tested.
History
Date User Action Args
2022-04-11 14:58:14adminsetgithub: 67977
2015-03-27 19:34:09mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg239430

resolution: not a bug
2015-03-27 15:00:38r.david.murraysetnosy: + skrah
2015-03-27 14:21:18paul.mooresetnosy: + paul.moore
messages: + msg239404
2015-03-27 13:01:13MonsieurPaulcreate