Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the integer part of a Decimal field in a Django template

My question is very simple, but I couldn't find an answer. I'm sorry if it has been answered before.

I have a DecimalField which came from a view to my template in my django application. I need to display only the integer part of it.

How can I do that?

========================= SOLUTION FOUND ========================

Sorry, I would have answered if I had permission, but only after 8 hours I can do so.

Thank you for the answers. I have found a builtin solution that worked just fine. It is a builtin template filter. The documentation follows.

https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#floatformat

Basically, the usage for my purpose is something like this:

{{decimal_obj|floatformat:0}}

An even better solution is the stringformat filter:

https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#stringformat

Usage:

{{decimal_obj|stringformat:"d"}}

Thanks for the help. Ignacio was right ;)

like image 782
Francisco Avatar asked Jan 19 '12 02:01

Francisco


1 Answers

If you only need to display the integral part of it then use a stringformat of "d". If you need to calculate it then do it in the view.

Example:

{{ decimal_field | stringformat:'d' }}

like image 127
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 18:09

Ignacio Vazquez-Abrams