@contextfilter
def number_format(context, value):
    """
    Enforces 2 decimal places after a number if only one is given (adds a zero)
    also formats comma separators every 3rd digit before decimal place.
    """
    value = str(value)

    negative = False
    addzero = None

    if value[0] == '-':
        value = value[1:]
        negative = True

    if '.' in value:
        point = value.rindex('.')
        if point == len(value) - 2:
            addzero = True
    else:
        point = len(value)

    # Ensure we don't get lots of zero's after '.'
    # Cut trailing zeros and only leave two
    while point < len(value) - 3:
        if value[len(value) - 1] == '0':
            value = value[:len(value) - 1]
        else:
            break

    while point > 3:
        value = value[:point - 3] + "," + value[point - 3:]
        point = value.index(',')

    if addzero:
        value += "0"

    if negative:
        value = "-" + value

    return value
dataset/ETHPy150Open treeio/treeio/treeio/core/templatetags/modules.py/number_format/original