When using Decimal(0)
and formatting it to .2e
format the following happens:
>>> f'{Decimal(0):.2E}'
'0.00E+2'
However if you just use 0
or 0.
the following happens:
>>> f'{0.:.2E}'
'0.00E+00'
How come the results are different?
In my previous answer I described how this is done in cpython, in this I will describe why. This question was raised in this discussion and most of the quotes will be from there:
Let's consider an example:
>>> x = Decimal("1e+5").quantize(Decimal("1e+10"))
>>> x
Decimal('0E+10')
>>> s = "{:.19e}".format(x)
>>> s
'0.0000000000000000000e+29'
>>> Decimal(s)
Decimal('0E+10')
The original magnitude was e+10
, after formatting it's still e+10
.
The magnitude of the original number is kept after the formatting process.
From the point of view of decimal it's the right thing. The original magnitude should be traceable
0
is really special in the IBM specification. The magnitude is kept, the precision is not.>>> Decimal("0e10") * Decimal("0e20") Decimal('0E+30') >>> Decimal("0.000e10") Decimal('0E+7')
So we're basically doing the reverse of the above in formatting when a precision is given.
So, if we go back to the original example of the OP:
>>> f'{Decimal(0):.2E}'
'0.00E+2'
If it returned 0.00Е+00
then the magnitude of this decimal number would be E-2
:
>>> d = f'{Decimal(0):.2E}'
>>> d
'0.00E+2'
>>> Decimal(d)
Decimal('0')
>>> d = '0.00E+00'
>>> Decimal(d)
Decimal('0.00')