Small corrections:

About 1) — actually decimal.Decimal(<float>) was an error Python 3.0 and 3.1.

About 2) — I mixed up two options for the factory to build decimals from floats:

- the decimal.Decimal.from_float class method — which I assume uses the global 
context
- the decimal.Context..create_decimal_from_float method — which relies on a 
specific context

https://docs.python.org/3.5/library/decimal.html#decimal.Decimal.from_float

I assume the latter will be more convenient for low level control.

-- 
Aymeric.

> On 05 Apr 2016, at 16:17, Aymeric Augustin 
> <aymeric.augus...@polytechnique.org> wrote:
> 
> I have three ideas about this.
> 
> 1) In my opinion, assigning a float to a decimal field is a programming 
> error. Sadly Python doesn’t raise an exception in that case.
> 
> >>> decimal.Decimal(2.15)
> Decimal('2.149999999999999911182158029987476766109466552734375')
> 
> So Django has to support it as well.
> 
> 2) Python 3 provides the `create_decimal_from_float(<float>)` class method on 
> `decimal.Context` for this purpose. I think that’s what Django should use.
> 
> >>> decimal.Context(prec=10).create_decimal_from_float(2.15)
> Decimal('2.150000000')
> 
> https://docs.python.org/3.5/library/decimal.html#decimal.Context.create_decimal_from_float
>  
> <https://docs.python.org/3.5/library/decimal.html#decimal.Context.create_decimal_from_float>
> 
> (I don’t care much about Python 2 at this point. We should just make sure we 
> don’t introduce a debatable behavior in the last LTS release supporting 
> Python 2.)
> 
> 3) Currently DecimalField accepts max_digits and decimal_places options. I 
> think it should accept a decimal context and delegate all operations to that 
> context.
> 
> I suggest the following behavior:
> 
> - start with the decimal context provided in a kwarg to DecimalField or, if 
> there is None, the current context returned by `getcontext()`
> - modify that context to take into account max_digits and decimal_places
> - ask the context to perform whatever operations we need
> 
> https://docs.python.org/3.5/library/decimal.html#decimal.getcontext 
> <https://docs.python.org/3.5/library/decimal.html#decimal.getcontext>
> 
> I’m -1 on emulating that with arbitrary string conversions and rounding rules 
> and also -1 on deviating from Python’s default behavior, which matches the 
> IEEE 754 specification, without providing a way to customize that behavior 
> properly.
> 
> I hope this helps !
> 
> -- 
> Aymeric.
> 
>> On 05 Apr 2016, at 15:49, Tim Graham <timogra...@gmail.com 
>> <mailto:timogra...@gmail.com>> wrote:
>> 
>> If you assign a float with more decimal places than a DecimalField field 
>> supports to that field, the value may round differently than if you assign 
>> the same value as a string. For example:
>> 
>> class Invoice(models.Model):
>>     gross = models.DecimalField(max_digits=10, decimal_places=1)
>> 
>> invoice.gross = 2.15    # saves as 2.1
>> invoice.gross = '2.15'  # saves as 2.2
>> 
>> The default behavior of decimal rounding is ROUND_HALF_EVEN (to nearest with 
>> ties going to nearest even integer). There's a proposal to change this to 
>> cast floats to string and then use ROUND_HALF_UP to match the value of 
>> strings [0][1]. Do you have any concerns about this? Is it something we 
>> should even care about?
>> 
>> [0] https://github.com/django/django/pull/6410 
>> <https://github.com/django/django/pull/6410>
>> [1] https://code.djangoproject.com/ticket/26459 
>> <https://code.djangoproject.com/ticket/26459>
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-developers+unsubscr...@googlegroups.com 
>> <mailto:django-developers+unsubscr...@googlegroups.com>.
>> To post to this group, send email to django-developers@googlegroups.com 
>> <mailto:django-developers@googlegroups.com>.
>> Visit this group at https://groups.google.com/group/django-developers 
>> <https://groups.google.com/group/django-developers>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-developers/3749bbaf-ded7-4de3-912b-3a9e654b0207%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-developers/3749bbaf-ded7-4de3-912b-3a9e654b0207%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/07CD3A6B-EC86-42CC-A6BC-03D74112A0C3%40polytechnique.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to