9

希望简单的问题得到一个简单的答案。我只想编写 Django 的过滤器,以便获得格式化的浮点数区域设置感知:

{{123.45|floatformat:1}}
"123.5"  <= correct
{{123.45|localize}}
"123,45"  <= correct, in my locales decimal separator is a comma
{{123.45|floatformat:1|localize}}
"123.5"  <= wrong, point instead of comma. Expected output: "123,5"

如何同时应用两个过滤器?

谢谢。

4

1 回答 1

6

Django's floatformat filter returns a string, not actually a float number, so when you pass that value to localize, you are passing a string, not a number (int, float, etc) and therefore just returns the given string.

To achieve what you want, you can't combine these two filters in Django, since both return a string. To do that, you would have to make your own filter which will do the behavior you want. To make things easier, you can always start with the code in the default filters and modify that to your specification. floatformat code is here and localize is here.

于 2012-11-12T00:11:16.260 回答