Skip to main content

A set of simple math filters for Django

Project description

Build status Coverage PyPI download stats

django-mathfilters is a pip-installable Python 2/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, Decimal and cDecimal types, or any other type that can be converted to int or float.

WARNING: Note that when you have the ``cdecimal`` package installed, I assume all ``Decimal`` values that are passed to the tag are of ``cdecimal`` type. If they aren’t, weird things may happen.

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:

{% load mathfilters %}

...

<h1>Basic math filters</h1>

<ul>
    <li>8 + 3 = {{ 8|add:3 }}</li>

    <li>13 - 17 = {{ 13|sub:17 }}</li>

    {% with answer=42 %}
    <li>42 * 0.5 = {{ answer|mul:0.5 }}</li>
    {% endwith %}

    {% with numerator=12 denominator=3 %}
    <li>12 / 3 = {{ numerator|div:denominator }}</li>
    {% endwith %}

    <li>|-13| = {{ -13|abs }}</li>
</ul>

Version Support

This module supports Python 2.6–2.7 and 3.2–3.4 as well as PyPy and PyPy3.

Supported Django versions are 1.1 through 1.8.

See https://travis-ci.org/dbrgn/django-mathfilters for the full build matrix.

License

MIT License, see LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-mathfilters-0.4.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

django_mathfilters-0.4.0-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-mathfilters-0.4.0.tar.gz.

File metadata

File hashes

Hashes for django-mathfilters-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e570e113a92dd54125fc2066a93b509a521d3f42a4502731775d0f7b92acc3e8
MD5 fc742306ef32d6478ac6536904dddbf6
BLAKE2b-256 b044a194b6374328a102ad8541bf63ba698177199a08ce184b99bf4916f26f01

See more details on using hashes here.

File details

Details for the file django_mathfilters-0.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_mathfilters-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 530774776e5bbf8061c95e3542166fa8a4ec4cd8992b6dc9f3cad866c71051b5
MD5 f940470566a76ed95aa194156c6f5a49
BLAKE2b-256 c6fbfa2fe3d531dc018d486b03c91461063e1005e31bb00b7f2ebc6dfa09701f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page