Decimal places in Django template

Asked

Viewed 883 times

0

Good morning, everyone,

I’m having a basic problem but apparently boring to solve:

{% load l10n %}
{% localize off %}
    var dados_grafico = [
        [
            'Entradas',
            {{relatorio.grafico.entradas|floatformat:2}},
            {{relatorio.grafico.saidas|floatformat:2}},
        ]
    ]
{% endlocalize %}

From Django’s documentation, assuming I have 10 for inputs and 7 for outputs, this should return 10.00 and 7.00 respectively, with dots separating the decimal places, which is what I desire.

The problem is it’s returning 10.00 and 7.00 (commas separating the decimal places).

I also noticed that by removing the floatformat:2 of the values, Django returns, for example, 10,000 and 7,000 (works but does not limit the number of decimals).

Question: Someone’s been through it and can give me a light?

2 answers

2

You can change this in your file settings.py, but note that for our system the "correct", is to separate decimal places with commas and not with point, the topic Format localization in the documentation says that formatting is disabled by default, so you should probably have the following command in your settings.py:

USE_L10N = True

Although local formatting is disabled by default, when Voce creates a project with django-admin startproject, by convenience, USE_L10N = True is included in your settings. Try to remove this line or switch to:

USE_L10N = False

Note that this will affect other details of local formatting (Brazil).

intcoma (Django.contrib.humanize):

One option is to use [django.contrib.humanize][2] "A set of template filters that adds a 'human touch' to the data"

intcomma Convert a int or float (or a string representation) for a string containing commas every 3 digits:

Examples:

4500 se torna 4,500
4500.2 se torna 4,500.2

I don’t know if this option would specifically suit your case, to use it, add on INSTALLED_APPS and the template you will use {% load humanize %}

  • So... Only in this case I would have to treat all the other numbers in my project so that they appear with the comma as the separator character of the decimal places. My intention was to make only this data set have this configuration, you understand?

  • 1

    @illozzaM I don’t understand. If you change your Settings, your whole project will work according to the new configuration, qq way, I added an option in the reply, by another Lada, if it is for a specific case, it would not be easier to convert to string in the format you want, in the back-end and send already formatted to the template?

  • Exactly, Sidon! I did something similar by converting via Javascript in my template. Thank you! :)

0

The obvious is often the last thing that comes to mind:

    var dados_grafico = [
        [
            'Entradas',
            parseFloat('{{relatorio.grafico.entradas|floatformat:2}}'.replace(',', '.')),
            parseFloat('{{relatorio.grafico.saidas|floatformat:2}}'.replace(',', '.')),
        ]
    ]

This way, Django will return the value with the comma but it is modified via Javascript by the dot. :-)

  • I didn’t understand, your first version of the question was already like this (that’s why I invested my time, finding that way n served p vc), you edited, and put the first version as an answer, to explain?

  • It wasn’t exactly like this. In the first version, I was using the input and output data without the quotes, including a '.toString()' for js to convert the value to string before replacing. This did not work because Django showed the value with such a comma in place of the desired point. Then add the quotes and remove the '.toString()' enables js to run '.replace()'. The other modifications were to make the answer more useful to other people who have a similar problem. I thank you so much for your help because you helped me find a solution!

  • Yes, after you edited the answer too, it was, say,,,, a little different, but I have no doubt that the answer is the question itself. :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.