https://github.com/dbrgn/django-mathfilters
django-mathfilters provides a set of simple math filters for Django.
https://github.com/dbrgn/django-mathfilters
Last synced: over 1 year ago
JSON representation
django-mathfilters provides a set of simple math filters for Django.
- Host: GitHub
- URL: https://github.com/dbrgn/django-mathfilters
- Owner: dbrgn
- License: mit
- Created: 2012-10-09T15:38:36.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T07:44:11.000Z (over 5 years ago)
- Last Synced: 2025-04-02T04:40:28.791Z (over 1 year ago)
- Language: Python
- Size: 46.9 KB
- Stars: 169
- Watchers: 8
- Forks: 29
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.rst
- License: LICENSE
Awesome Lists containing this project
README
##################
django-mathfilters
##################
.. image:: https://github.com/dbrgn/django-mathfilters/workflows/CI/badge.svg
:alt: Build status
:target: https://github.com/dbrgn/django-mathfilters/actions?query=branch%3Amaster
.. image:: https://pypip.in/d/django-mathfilters/badge.png
:alt: PyPI download stats
:target: https://crate.io/packages/django-mathfilters
django-mathfilters is a Python 3 module that provides different simple math
filters for Django.
Django provides an ``add`` template filter, but no corresponding subtracting,
multiplying or dividing filters.
Django ticket `#361 `_ has been
closed as *wontfix*, so I had to create an alternative that is easy to install
in a new Django project.
It currently supports ``int``, ``float`` and ``Decimal`` types, or any other
type that can be converted to int or float.
Installation
============
::
$ pip install django-mathfilters
Then add ``mathfilters`` to your ``INSTALLED_APPS``.
Usage
=====
You need to load ``mathfilters`` at the top of your template. The script
provides the following filters:
* ``sub`` – subtraction
* ``mul`` – multiplication
* ``div`` – division
* ``intdiv`` – integer (floor) division
* ``abs`` – absolute value
* ``mod`` – modulo
* ``addition`` – replacement for the ``add`` filter with support for float /
decimal types
**Example:**
.. sourcecode:: html
{% load mathfilters %}
...
Basic math filters
- 8 + 3 = {{ 8|add:3 }}
- 13 - 17 = {{ 13|sub:17 }}
- 42 * 0.5 = {{ answer|mul:0.5 }}
- 12 / 3 = {{ numerator|div:denominator }}
- |-13| = {{ -13|abs }}
{% with answer=42 %}
{% endwith %}
{% with numerator=12 denominator=3 %}
{% endwith %}
Version Support
===============
This module officially supports Python 3.5+ as well as PyPy3. Support for Python
3.3 and 3.4 is provided on best-effort basis, but there are no CI tests for it.
Supported Django versions are 1.11+, 2.x and 3.x.
Development
===========
This project uses `Black `__ for
auto-formatting. Adherence to the rules is enforced in CI.
License
=======
`MIT License `_, see LICENSE file.