# [Python-Dev] Decimal data type issues

Ka-Ping Yee python-dev at zesty.ca
Tue Apr 13 23:29:39 EDT 2004

```Facundo Batista wrote:
> c. from_string
[...]
> Regarding the method (c), the only difference with creating
> the decimal with Decimal(string) is that method (c) honors
> the context (if the literal contains more digits that the
> current precision the numbers get rounded, and gets rounded
> according to the round method specified in context, etc).
> For example, with a precision of 9 and with the name I proposed::
>
> >>> Decimal('112233445566')
> Decimal( (0, (1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6), 0) )
> >>> Decimal.from_string('112233445566')
> Decimal( (0, (1, 1, 2, 2, 3, 3, 4, 4, 6), 3L) )

On Wed, 14 Apr 2004, Tony Meyer wrote:
> As a relative newbie, I think that it would be better if (c) had a name that
> somehow indicated the difference; however, I can't think of one offhand that
> does :)  It seems to me that it would be easy to assume that the two above
> cases were the same (and given that they produce the same result in some
> situations, might be missed by weak testing).  Certainly a quick look at
> documentation would correct that, but if the name could also reflect the
> difference, that would be better.
>
> Even if from_string took a parameter "honour_context", defaulting to True,
> that would, IMO, be more clear that there's a difference between the two,
> and what it is.

I agree with Tony.  Having both Decimal() and Decimal.from_string() work
in similar but subtly different ways seems likely to be misunderstood.

I also think his last suggestion could provide a nice, convenient solution
to the conversion-from-float issue, since it raises the same question (a
method named "Decimal.from_float" doesn't explain how or why it is different
from just constructing with a float).

So here's a more detailed suggestion.  Suppose s = '12345', f = 12.345,
the default context specifies a precision of 9 as given in the PEP, and
c is a context object with a precision of 6.

Then this:                      would yield this:

Decimal(s)                      Decimal((0, (1, 2, 3, 4, 5), 0))
Decimal(s, 2)                   Decimal((0, (1, 2), 4))
Decimal(s, context=c)           Decimal((0, (1, 2, 3, 4, 5, 0), -1))
Decimal(s, default_context=1)   Decimal((0, (1, 2, 3, 4, 5, 0, 0, 0, 0), -4))

Decimal(f)                      Decimal((0, (1, 2, 3, 4, 5, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 6, 3, 9,
4, 8, 8, 4, 6, 2, 1, 8, 4, 0,
9, 0, 1, 6, 7, 2, 8, 4, 0, 1,
1, 8, 4, 0, 8, 2, 0, 3, 1, 2,
5), -49))
Decimal(f, 2)                   Decimal((0, (1, 2), 0))
Decimal(f, context=c)           Decimal((0, (1, 2, 3, 4, 5, 0), -4))
Decimal(f, default_context=1)   Decimal((0, (1, 2, 3, 4, 5, 0, 0, 0, 0), -6))