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)