在Javascript中将字节转换为KB / MB

时间:2018-03-21 07:48:44

标签: javascript html

我想转换目前以字节为单位的文件大小MB格式。

以下是我的代码:

SELECT a.ID,a.Type,a.Date,b.Status1,a.Status2,a.Status3 
From Table1 a
inner join Table2 b
on a.abc=b.xyz
inner join Table2 c
on a.efg=c.xyz
GROUP BY a.Type
HAVING Count(a.Type)>0 
AND b.Status1='AAA'
UNION SELECT a.ID,a.Type,a.Date,b.Status1,a.Status2,a.Status3
FROM Table1 a
inner join Table2 b
on a.abc=b.xyz
inner join Table2 c
on a.efg=c.xyz
GROUP BY a.Type
HAVING Count(a.Type)>0
AND a.Date > (SELECT Date From Table1 GROUP BY Type HAVING b.Status1='AAA' AND Count(Type)>0)
AND ( Status2='A' AND Status3='D' )
OR Status3='D';

的输出
var x = document.getElementById("file");
var txt = "";
var totalSize = 0;

if ('files' in x) {
    if (x.files.length == 0) {
        txt = "Select one or more files.";
    } else {
        for (var i = 0; i < x.files.length; i++) {
            txt += "<br><strong>" + (i+1) + ". file</strong><br>";
            var file = x.files[i];
            if ('name' in file) {
                txt += "name: " + file.name + "<br>";
            }
            if ('size' in file) {
                totalSize += file.size;
                txt += "size: " + file.size + " bytes <br>";
            }
        }
    }
}
document.getElementById ("displayTotalSize").innerHTML = totalSize;
document.getElementById ("displaySize").innerHTML = txt;

正确地出现在字节中:

document.getElementById ("displayTotalSize").innerHTML = totalSize;

现在我希望将其转换为MB。

请帮帮我。

1 个答案:

答案 0 :(得分:2)

你需要将totalSize划分为1024 ^ 2表示MB,对于需要1024 ^ 1的KB,对于GB,你应该划分为1024 ^ 4

var totalSizeKB = totalsize / Math.pow(1024,1)
var totalSizeMB = totalsize / Math.pow(1024,2)
var totalSizeGB = totalsize / Math.pow(1024,3)

哪个会给你3.000241279602051MB