Wednesday, January 8, 2014

Re: DecimalField Problem



On Thursday, January 9, 2014 7:37:43 AM UTC+11, Timothy W. Cook wrote:

On Wed, Jan 8, 2014 at 6:07 PM, Erik Cederstrand <erik+...@cederstrand.dk> wrote:
Den 08/01/2014 kl. 17.56 skrev Daniel Roseman <dan...@roseman.org.uk>:
<snip> 
>
But to post the solution to the issue, just for completeness. 

Yes, Django does store the field contents as decimal.Decimal objects.  In your templates or if writing out to a file (as I need to do) you need to use the formatting option. 

Example using the previously used model field:

str('%.10g' % self.min_inclusive).strip()
  
This removes all non-significant zeros. So it gives me '0' for a zero.  

I use a slightly different approach to chop unwanted zeroes ...

def chop_zeros(val):
    if isinstance(val, decimal.Decimal):
        return float(val)
    return val

... which I call for *all* numeric values before I do any string formatting. Which is why it works for me.

Mike


 


--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc           +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2207740c-d3ca-4998-a834-6f146231a4cd%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate