在上傳文件時(shí),獲取的文件大小往往是多少Byte, 顯示時(shí)經(jīng)常需要夠K的顯示為多少K, 夠M的顯示為M,夠G的顯示為G, 這里就需要一個(gè)轉(zhuǎn)換
function getfilesize(size) { if (!size) return ""; var num = 1024.00; //byte if (size < num) return size + "B"; if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + "K"; //kb if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M if (size < Math.pow(num, 4)) return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T }