Formats the value using the old printf-style string formatting.
Variable
pi = math.pi
Template
{{ pi|stringformat:'E' }}
Result
3.141593E+00
Argument: format
(required)
– the string format.
Formats the value using the old printf-style string formatting.
pi = math.pi
{{ pi|stringformat:'E' }}
3.141593E+00
Did we get something wrong? Is there a use case for the
stringformat
filter that we should add?
Please let us know.
Commentary
For some types of websites, this might be super useful, but we find our strings are usually stored in the format in which we want to output them. And for numbers
floatformat
is generally more useful.One case where
stringformat
can be useful: comparing a value on the querystring, which is always a string, with an integer property of an object (e.g., an id). For example:Thanks to Simeon Visser for this Stack Overflow answer.