Skip to content

Instantly share code, notes, and snippets.

@eads
Last active February 29, 2016 21:57
Show Gist options
  • Save eads/6734e0fdd73fd58c721c to your computer and use it in GitHub Desktop.
Save eads/6734e0fdd73fd58c721c to your computer and use it in GitHub Desktop.
from decimal import Decimal, ROUND_DOWN
def percent_filter(value):
"""
Format percentage
"""
if value == 0:
return '0%'
elif value == 100:
return '100%'
elif value > 0 and value < 1:
return '<1%'
else:
return '{:.1f}%'.format(Decimal(value).quantize(Decimal('.1'), rounding=ROUND_DOWN))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment