Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #18645 -- Clarified filesizeformat implementation

Thanks Jérôme Renard for the patch.
  • Loading branch information...
commit 1e89a208d0d741558c31a161222e8aa96764e08c 1 parent 810fd23
@aaugustin aaugustin authored
Showing with 16 additions and 10 deletions.
  1. +16 −10 django/template/defaultfilters.py
View
26 django/template/defaultfilters.py
@@ -827,17 +827,23 @@ def filesizeformat(bytes):
filesize_number_format = lambda value: formats.number_format(round(value, 1), 1)
- if bytes < 1024:
+ KB = 1<<10
+ MB = 1<<20
+ GB = 1<<30
+ TB = 1<<40
+ PB = 1<<50
+
+ if bytes < KB:
return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
- if bytes < 1024 * 1024:
- return ugettext("%s KB") % filesize_number_format(bytes / 1024)
- if bytes < 1024 * 1024 * 1024:
- return ugettext("%s MB") % filesize_number_format(bytes / (1024 * 1024))
- if bytes < 1024 * 1024 * 1024 * 1024:
- return ugettext("%s GB") % filesize_number_format(bytes / (1024 * 1024 * 1024))
- if bytes < 1024 * 1024 * 1024 * 1024 * 1024:
- return ugettext("%s TB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024))
- return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
+ if bytes < MB:
+ return ugettext("%s KB") % filesize_number_format(bytes / KB)
+ if bytes < GB:
+ return ugettext("%s MB") % filesize_number_format(bytes / MB)
+ if bytes < TB:
+ return ugettext("%s GB") % filesize_number_format(bytes / GB)
+ if bytes < PB:
+ return ugettext("%s TB") % filesize_number_format(bytes / TB)
+ return ugettext("%s PB") % filesize_number_format(bytes / PB)
@register.filter(is_safe=False)
def pluralize(value, arg='s'):
Please sign in to comment.
Something went wrong with that request. Please try again.