diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py
index 4aa5a32..efb1638 100644
a
|
b
|
from django import template
|
4 | 4 | from django.template import defaultfilters |
5 | 5 | from django.conf import settings |
6 | 6 | from datetime import date, timedelta |
| 7 | import math |
7 | 8 | import re |
8 | 9 | |
9 | 10 | register = template.Library() |
… |
… |
def naturalday(value, arg=None):
|
98 | 99 | return _(u'yesterday') |
99 | 100 | return defaultfilters.date(value, arg) |
100 | 101 | register.filter(naturalday) |
| 102 | |
| 103 | def filesizeformat(bytes, precision=2): |
| 104 | """ Returns a human readable string of a byte count. """ |
| 105 | bytes = int(bytes) |
| 106 | if bytes is 0: |
| 107 | return u'0%s' % _(u'bytes') |
| 108 | log = math.floor(math.log(bytes, 1024)) |
| 109 | return "%.*f%s" % ( |
| 110 | precision, |
| 111 | bytes / math.pow(1024, log), |
| 112 | [_(u'bytes'), u'kb', u'mb', u'gb', u'tb', u'pb', u'eb', u'zb', u'yb'][int(log)] |
| 113 | ) |
| 114 | register.filter(filesizeformat) |
| 115 | |
diff --git a/docs/add_ons.txt b/docs/add_ons.txt
index 029e314..b389649 100644
a
|
b
|
Values up to 1000000000000000 (one quadrillion) are supported.
|
141 | 141 | |
142 | 142 | You can pass in either an integer or a string representation of an integer. |
143 | 143 | |
| 144 | filesizeformat |
| 145 | -------------- |
| 146 | |
| 147 | Converts a byte count into a human readable string like "2Mb". Accepts an |
| 148 | optional ``precision`` parameter which defaults to 2. |
| 149 | |
| 150 | Examples: |
| 151 | |
| 152 | * ``3352576`` becomes ``3.20mb`` |
| 153 | * ``623394`` becomes ``608.78kb`` |
| 154 | |
144 | 155 | ordinal |
145 | 156 | ------- |
146 | 157 | |