On Wed, May 11, 2011 at 1:14 AM, Colin Corbett <[email protected]>wrote:
> Thanks! Solved:
>
> In models.py:
>
> class OurProducts(models.Model):
> code = models.CharField(max_length=60)
> name = models.CharField(max_length=80)
> price = models.DecimalField (max_digits=8, decimal_places=2)
>
> def combined_price(self):
> from decimal import Decimal
> return round(self.price * Decimal(0.65),2)
>
For the sake of anyone searching the group for this issue in the future,
that last method should be:
> def combined_price(self):
> from decimal import Decimal
return (self.price * Decimal('0.65')).quantize(Decimal('0.01'))
Python will sensibly throw an exception if you try to create a Decimal from
a float directly, so Decimal(0.65) will not work. Also, the round() function
will turn your carefully calculated decimal value *back* into a float; you
should use .quantize() instead.
--
Regards,
Ian Clelland
<[email protected]>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.