From 022febcd72300120fd20b4df8652d9afbd9817ce Mon Sep 17 00:00:00 2001 From: Jaime Pillora Date: Sun, 15 Oct 2017 13:02:35 +1100 Subject: [PATCH] fix frontend "bytes" formatter --- static/files/js/utils.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/static/files/js/utils.js b/static/files/js/utils.js index 2eed31bc8..29e0e0ea9 100644 --- a/static/files/js/utils.js +++ b/static/files/js/utils.js @@ -86,14 +86,14 @@ app.filter("bytes", function(bytes) { app.factory("bytes", function() { return function(n, d) { // set defaults - if (typeof n !== "number" || isNaN(n)) n = 0; + if (typeof n !== "number" || isNaN(n) || n == 0) return "0 B"; if (!d || typeof d !== "number") d = 1; // set scale index 1000,100000,... becomes 1,2,... - var i = Math.floor(Math.floor(Math.log10(n)) / 3); + var i = Math.floor(Math.floor(Math.log(n) * Math.LOG10E) / 3); + // set rounding factor var f = Math.pow(10, d); - var s; - s = n / Math.pow(10, i * 3); - s = Math.round(s * f) / f; + // scale n down and round + var s = Math.round(n / Math.pow(10, i * 3) * f) / f; // concat (no trailing 0s) and choose scale letter return ( s.toString().replace(/\.0+$/, "") +