Django template: localize number returned from simple tag
Question: I hope this question is not here, I have googled it for long time and not
found anything. I have problem with formating numbers in templates, but
somehow special. Maybe, this is whole problem how to call filters on values
returned form simple tags. So, first, some facts:

  * using python 2.7 and Django 1.5
  * In czech language, we are using comma "," as decimal separator and optionaly space " " as thousand separator (more often is to write numbers without thousand separator).
  * Normally, writing number by `{{ price }}` is printing e.g. **16,0**

For some reason, I have to work with numbers in template:

    
    
    {% count_basic price.leafletPrice.price product.amount product.amount_unit product.category.basic_amount product.category.basic_amount_unit %}
    

count basic is simple tag:

    
    
    from django import template
    
    register = template.Library()
    
    @register.simple_tag
    def count_basic(price, product_amount, product_unit, category_amount, category_unit):
        if product_unit == 'mililiter' or product_unit == 'gram':
            return (float(price) / product_amount) * 1000
        else:
            return float(price) / product_amount
    

Result is that is't printed as **16.0**. I have set l10n in `settings.py`to
True:

    
    
    USE_I18N = True
    USE_L10N = True
    

I have also found "humanize" as working but firstly, I don't know how to call
simple tag/filter with value returned from another simple tag (maybe using
`with`?), secondly, if it's working for normal numbers, why not for numbers
returned from simple tag? Is the number badly returned? Should be converted? I
have tried also return value as `Decimal()` and not working.

Any easy working solution appreciated.


Answer: OK, finally, I have found out how to do this. I have changed my simple tag,
specially the return part(s) to:

    
    
    from django.template.defaultfilters import floatformat
    
    return floatformat(float(price) / product_amount, 2)
    

`floatformat` made template to display number formated, with comma as decimal
separator. Two decimal places (second parameter of floatformat) was requested.

