close
function readablizeBytes(bytes) {
var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
var e = Math.floor(Math.log(bytes)/Math.log(1024));
return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}

====================================================================

readablizeBytes(5000); // 4.88 kb

以上程式來源:http://www.elctech.com/snippets/convert-filesize-bytes-to-readable-string-in-javascript

最近在寫byte轉kb, MB, GB...的程式
正在google Math時找到的
看來我的數學很差…都沒有想過可以這樣子寫
arrow
arrow
    全站熱搜

    otnv 發表在 痞客邦 留言(0) 人氣()