Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

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.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.