Significant digits filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django import template
import math

register = template.Library()

def sigdig(value, digits = 3):
    order = int(math.floor(math.log10(math.fabs(value))))
    places = digits - order - 1
    if places > 0:
        fmtstr = "%%.%df" % (places)
    else:
        fmtstr = "%.0f"
    return fmtstr % (round(value, places))


register.filter('sigdig', sigdig)

More like this

  1. HTML to text filter by MasonM 3 years, 6 months ago
  2. Calendar table by fauxparse 3 years, 9 months ago
  3. Jinja2 Django integration by mathwizard 3 years, 4 months ago
  4. Convert newlines into <p> and </p> tags by jheasly 4 years, 10 months ago
  5. Bitwise operator queryset filter by hgeerts@osso.nl 1 year, 9 months ago

Comments

(Forgotten your password?)