文、意如
電腦的最小單位為 Bit (位元)
8個位元(bit)等於1個位元組(Byte)
1 Byte (B) = 8 bit
1 Kilobyte (KB) = 1024 Bytes
1 Megabyte (MB) = 1024 KB
1 Gigabyte (GB) = 1024 MB
1 Terabyte (TB) = 1024 GB
1 Petabyte (PB) = 1024 TB
1 Exabyte (EB) = 1024 PB
1 Zettabyte (ZB) = 1024 EB
1 Yottabyte (YB) = 1024 ZB
1 Ronnabyte (RB) = 1024 YB
1 Quettabyte (QB) = 1024 RB
參考摘自維基百科的對照表:
Byte轉儲存單位
let statnum = 2304786;//byte
let decimal = 2;//小數點
let k = 1000; //or 1024
let unitsize: string[] = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'RB', 'QB'];
let r = Math.floor(Math.log(statnum) / Math.log(k)); //2
let resunit = (statnum / Math.pow(k, r)).toPrecision(decimal) +" "+ unitsize[r]; // 2.3 MB
console.log("resunit", resunit); //2.3
console.log("unitsize[r]", unitsize[r]); //MB
Yiru@Studio - 關於我 - 意如