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. Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits) by pikhovkin 1 year, 2 months ago
  2. FloatField with safe expression parsing by joelegner 3 years, 2 months ago
  3. Calendar table by fauxparse 5 years, 1 month ago
  4. Time ranges like 7-9 p.m. by sgb 5 years, 4 months ago
  5. Paginator Tag by insin 6 years, 2 months ago

Comments

(Forgotten your password?)