Formatting floats with decimal dots and limited decimal part in Django templates
Asked Answered
C

2

9

I know I can limit the number of decimals in a float by using the filter floatformat:2 which output a localized float and also I know the filter stringformat:"f" which outputs a dotted float like 1.54 instead of a localized comma float like 1,54.

For instance, if the original float is 1.54233 I would like to print 1.54 and not 1,54 or 1.54233. Can this be achieved without the need of a custom filter?

Cordi answered 11/12, 2015 at 20:0 Comment(3)
Not sure if I understand your question. floatformat is not a custom filter, why not use it?Hagai
@ShangWang because floatformat returns a localized float using commas instead of points (or dots)Cordi
you can change the locale in a block with some tags see docs.djangoproject.com/en/1.9/topics/i18n/formatting/#localizeBonspiel
B
13

just use the localize/unlocalize format separator

https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#std:templatefilter-localize

For example:

{% load l10n %}

{{ value|localize }}

To disable localization on a single value, use unlocalize. To control localization over a large section of a template, use the localize template tag. unlocalize¶

Forces a single value to be printed without localization.

For example:

{% load l10n %}

{{ value|unlocalize }}

To force localization of a single value, use localize. To control localization over a large section of a template, use the localize template tag.

edit:

see https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#switching-language-in-templates

{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<p>{% trans "Welcome to our page" %}</p>

{% language 'en' %}
    {% get_current_language as LANGUAGE_CODE %}
    <!-- Current language: {{ LANGUAGE_CODE }} -->
    <p>{% trans "Welcome to our page" %}</p>
{% endlanguage %}

you can switch languages to force the display if localize/unlocalize does not work

Bonspiel answered 11/12, 2015 at 20:11 Comment(6)
can I use this after a floatformat filter? like {{ val|floaformat:2|unlocalize}} ?Cordi
for some reason it is not working, I mean the floats are still being printed with a comma as decimal separator even using the localize off I think the floatfomat filter is not considering itCordi
It's weird, I had to disable I10N for the commas to stop showing but the unlocalize didn't workedCordi
Testing a little bit more I confirmed that floatformat indeed Ignores the unlocalize filter and the localize off tag, so what to do there?Cordi
specify a custom separator( see links above) and file a bug report at django on githubBonspiel
Using floatformat with language still works, so you can do {% language 'en'%}{{ value|floatformat:'1' }}{% endlanguage %} to get an "unlocalized" value. if you need a real numberCalfskin
A
23

Note, that localization (and therefore the unlocalize filter and localize tags) have NO effect on the output of floatformat! At the time of writing there is an open issue about better documentation.

While switching the language to "en" is a workaround, it is not necessary to achieve (a) always using a dot and (b) limiting the number of decimals and in my opinion taking advantage of a language feature side-effect is less than ideal.

To properly format a float with Django template filters independent of localization you can use stringformat! Printf-style formatting does not only accept a single conversion (like "f"), but several optional parameters like "precision". See the linked Python docs for details.

To format your float 1.54233 as 1.54 simply use:

{{ float_value|stringformat:".2f" }}
Annuitant answered 7/11, 2019 at 20:7 Comment(0)
B
13

just use the localize/unlocalize format separator

https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#std:templatefilter-localize

For example:

{% load l10n %}

{{ value|localize }}

To disable localization on a single value, use unlocalize. To control localization over a large section of a template, use the localize template tag. unlocalize¶

Forces a single value to be printed without localization.

For example:

{% load l10n %}

{{ value|unlocalize }}

To force localization of a single value, use localize. To control localization over a large section of a template, use the localize template tag.

edit:

see https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#switching-language-in-templates

{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<p>{% trans "Welcome to our page" %}</p>

{% language 'en' %}
    {% get_current_language as LANGUAGE_CODE %}
    <!-- Current language: {{ LANGUAGE_CODE }} -->
    <p>{% trans "Welcome to our page" %}</p>
{% endlanguage %}

you can switch languages to force the display if localize/unlocalize does not work

Bonspiel answered 11/12, 2015 at 20:11 Comment(6)
can I use this after a floatformat filter? like {{ val|floaformat:2|unlocalize}} ?Cordi
for some reason it is not working, I mean the floats are still being printed with a comma as decimal separator even using the localize off I think the floatfomat filter is not considering itCordi
It's weird, I had to disable I10N for the commas to stop showing but the unlocalize didn't workedCordi
Testing a little bit more I confirmed that floatformat indeed Ignores the unlocalize filter and the localize off tag, so what to do there?Cordi
specify a custom separator( see links above) and file a bug report at django on githubBonspiel
Using floatformat with language still works, so you can do {% language 'en'%}{{ value|floatformat:'1' }}{% endlanguage %} to get an "unlocalized" value. if you need a real numberCalfskin

© 2022 - 2024 — McMap. All rights reserved.