Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed do_filesizeformat to actually calculate correctly, fixes #59

  • Loading branch information...
commit 7d268bef0e8f3f12c0acb90f30d67726a3e3f261 1 parent 898975d
@EnTeQuAk authored
Showing with 23 additions and 5 deletions.
  1. +4 −4 jinja2/filters.py
  2. +19 −1 jinja2/testsuite/filters.py
View
8 jinja2/filters.py
@@ -361,10 +361,10 @@ def do_filesizeformat(value, binary=False):
return '%d Bytes' % bytes
else:
for i, prefix in enumerate(prefixes):
- unit = base ** (i + 1)
- if bytes <= unit:
- return '%.1f %s' % ((bytes / unit), prefix)
- return '%.1f %s' % ((bytes / unit), prefix)
+ unit = base ** (i + 2)
+ if bytes < unit:
+ return '%.1f %s' % ((base * bytes / unit), prefix)
+ return '%.1f %s' % ((base * bytes / unit), prefix)
def do_pprint(value, verbose=False):
View
20 jinja2/testsuite/filters.py
@@ -86,9 +86,27 @@ def test_filesizeformat(self):
out = tmpl.render()
self.assert_equal(out, (
'100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'
- '1000 Bytes|1.0 MiB|0.9 GiB|0.9 TiB'
+ '1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
))
+ def test_filesizeformat_issue59(self):
+ tmpl = env.from_string(
+ '{{ 300|filesizeformat }}|'
+ '{{ 3000|filesizeformat }}|'
+ '{{ 3000000|filesizeformat }}|'
+ '{{ 3000000000|filesizeformat }}|'
+ '{{ 3000000000000|filesizeformat }}|'
+ '{{ 300|filesizeformat(true) }}|'
+ '{{ 3000|filesizeformat(true) }}|'
+ '{{ 3000000|filesizeformat(true) }}'
+ )
+ out = tmpl.render()
+ self.assert_equal(out, (
+ '300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|'
+ '2.9 KiB|2.9 MiB'
+ ))
+
+
def test_first(self):
tmpl = env.from_string('{{ foo|first }}')
out = tmpl.render(foo=range(10))
Please sign in to comment.
Something went wrong with that request. Please try again.