Tutorials References Exercises Videos Menu
Create Website Get Certified Upgrade

Template Filter - floatformat


Example

Round a number to only two decimals:

<h1>{{ mynumber|floatformat:2 }}</h1>
Run Example »

Definition and Usage

The floatformat filter rounds a floating-point number to the specified number of decimals, or one, if no decimal argument is specified.

If the argument value is a positive number, the value will be displayed with the specified number of decimals, even if the value is an integer:

Example

<p>{{ 7.122489|floatformat:2 }}</p>
<p>{{ 7.1|floatformat:2 }}</p>
<p>{{ 7|floatformat:2 }}</p>
Run Example »

If the argument value is a negative number, the value will be displayed with the specified number of decimals, only if the value has decimals:

Example

<p>{{ 7.122489|floatformat:-2 }}</p>
<p>{{ 7.1|floatformat:-2 }}</p>
<p>{{ 7|floatformat:-2 }}</p>
Run Example »

You can add a g in the argument value to specify that the result should include a separator for each thousand:

Example

Round a number to only two decimals, and add a thousand separator:

<h1>{{ mynumber|floatformat:"2g" }}</h1>
Run Example »

Syntax

{{ value|floatformat:decimalsg }}

Template filters are defined by using a pipe | character followed by the name of the filter.

Arguments are defined by using a colon : character followed by the argument value(s).


Arguments

Value Description
decimals Optional. A number specifying the number of decimals. Default value is -1, which will round the number to only one decimal, or no decimals if the number has no decimals.
g Optional. If present, the number will be displayed with a separator for each thousand, using local settings (using , for English and . for Norwegian.
u Optional. If present, the number will be displayed with a separator for each thousand, without using local settings. If u is present g cannot be present.