diff --git a/copyparty/web/up2k.js b/copyparty/web/up2k.js index f24b9a6b..eb320600 100644 --- a/copyparty/web/up2k.js +++ b/copyparty/web/up2k.js @@ -236,7 +236,7 @@ function U2pvis(act, btns) { p = r.perc(nb, 0, fobj.size, fobj.t_hashing); fo.hp = '{0}%, {1}, {2} MB/s'.format( - p[0].toFixed(2), p[1], p[2].toFixed(2) + f2f(p[0], 2), p[1], f2f(p[2], 2) ); if (!r.is_act(fo.in)) return; @@ -258,7 +258,7 @@ function U2pvis(act, btns) { var p = r.perc(fo.bd, fo.bd0, fo.bt, fobj.t_uploading); fo.hp = '{0}%, {1}, {2} MB/s'.format( - p[0].toFixed(2), p[1], p[2].toFixed(2) + f2f(p[0], 2), p[1], f2f(p[2], 2) ); if (!r.is_act(fo.in)) @@ -903,7 +903,7 @@ function up2k_init(subtle) { td = (now - (etaref || now)) / 1000.0; etaref = now; - ebi('acc_info').innerHTML = st.time.busy.toFixed(1) + ' ' + (now / 1000).toFixed(1); + ebi('acc_info').innerHTML = f2f(st.time.busy, 1) + ' ' + f2f(now / 1000, 1); if (!nhash) ebi('u2etah').innerHTML = 'Done ({0}, {1} files)'.format(humansize(st.bytes.hashed), pvis.ctr["ok"] + pvis.ctr["ng"]); diff --git a/copyparty/web/util.js b/copyparty/web/util.js index a37cb28b..8bfe697b 100644 --- a/copyparty/web/util.js +++ b/copyparty/web/util.js @@ -401,16 +401,20 @@ function s2ms(s) { } +function f2f(val, nd) { + // 10.toFixed(1) returns 10.00 for certain values of 10 + val = (val * Math.pow(10, nd)).toFixed(0).split('.')[0]; + return nd ? (val.slice(0, -nd) || '0') + '.' + val.slice(-nd) : val; +} + + function humansize(b, terse) { - var i=0, u=terse? ['B','K','M','G'] : ['B','KB','MB','GB']; - while (b >= 1000 && i=100) return Math.floor(b) + u; - if (b>=10) return b.toFixed(1) + u; - return b.toFixed(2) + u; + var i = 0, u = terse ? ['B', 'K', 'M', 'G'] : ['B', 'KB', 'MB', 'GB']; + while (b >= 1000 && i < u.length) { + b /= 1024; + i += 1; + } + return f2f(b, b >= 100 ? 0 : b >= 10 ? 1 : 2) + ' ' + u[i]; }