[Python-Dev] Re: Decimal data type issues
Tim Peters
tim.one at comcast.net
Wed Apr 21 20:11:24 EDT 2004
[Batista, Facundo]
> ...
> Well, I think we must decide how it works
>
> With:
>
> >>> d = Decimal('12345.678')
> >>> d
> Decimal( (0, (1, 2, 3, 4, 5, 6, 7, 8), -3) )
> >>> str(d)
> '12345.678'
>
> And being the syntax Decimal.round(n), we have the
> following options:
>
> a) n is the quantity of relevant digits of the
> final number (must be non negative).
>
> >>> d.round(4)
> Decimal( (0, (1, 2, 3, 5), 1L) )
> >>> str(d.round(4))
> '1.235E+4'
>
> b) n has the same behaviour that in the built in round().
>
> >>> d.round(1)
> Decimal( (0, (1, 2, 3, 4, 5, 7), -1L) )
> >>> str(d.round(1))
> '12345.7'
> >>> d.round(-1)
> Decimal( (0, (1, 2, 3, 5), 1L) )
> >>> str(d.round(-1))
> '1.235E+4'
>
> What option do you all like more?
I like (c) best: drop round(), because it's redundant -- there are other
ways to do (a) and (b) already. Perhaps (b) isn't obvious, so extending
your example with what already exists:
>>> dimes = Decimal.Decimal('0.1')
>>> print d.quantize(dimes)
12345.7
>>> print d.quantize(Decimal.Decimal('1e1'))
1.235E+4
>>>
As a bonus, the spec defines quantize in all cases (including infinite and
NaN inputs, the effects on the large mass of condition flags, and so on).
In general, we should be very reluctant to extend the spec at the start:
it's complicated, subtle and exacting, and plenty of big brains are working
hard on getting the spec exactly right. Alas, it's also still a bit of a
moving target.
More information about the Python-Dev
mailing list