天天看點

根據輸入的byte機關計算出最簡潔的表示資料

根據輸入的byte機關計算出最簡潔的表示資料

/** 

* 根據輸入的byte機關,計算出最簡潔的表示資料 

* 例如1024 得 1k 

*/  

function humanreadablesize(f_size) {  

    return getsmartsize(f_size);  

}  

function getsmartsize(f_size) {  

    var funit, k_size, m_size, g_size, f_size;  

    k_size = f_size / 1024;  

    m_size = k_size / 1024;  

    g_size = m_size / 1024;  

    if (1024 > f_size) {  

        funit  = 'b';  

        r_size = f_size;  

    } else if (1024 > k_size) {  

        funit  = 'k';  

        r_size = math.round(k_size);  

    } else if (1024 > m_size) {  

        funit = 'm';  

        r_size = math.round(m_size * 10) / 10;  

    } else {  

        funit = 'g';  

        r_size = math.round(g_size * 10) / 10;  

    }  

    return '' + r_size + funit;  

function gen_size(val, li, sepa ) {  

    sep = math.pow(10, sepa); //小數點後的位數  

    li = math.pow(10, li); //開始截斷的長度  

    retval  = val;  

    unit    = 'bytes';  

    if (val >= li*1000000000) {  

        val = math.round( val / (1099511627776/sep) ) / sep;  

        unit  = 'tb';  

    } else if (val >= li*1000000) {  

        val = math.round( val / (1073741824/sep) ) / sep;  

        unit  = 'gb';  

    } else if (val >= li*1000) {  

        val = math.round( val / (1048576/sep) ) / sep;  

        unit  = 'mb';  

    } else if (val >= li) {  

        val = math.round( val / (1024/sep) ) / sep;  

        unit  = 'kb';  

    return val + unit;