Topic: [Javascript]convert file size to readable string

function get_file_size_str(file_size) {
    sizes  = [["B", 1], ["KB", 1024.0], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]];
    for(ii = sizes.length - 1; ii >= 0; ii--) {
        if(file_size >= sizes[ii][1]) {
            str_file_size = (file_size / sizes[ii][1]) + "";
            pos = str_file_size.indexOf('.');
            if(pos > 0)
                str_file_size = str_file_size.substr(0, pos + 3);
            return str_file_size + sizes[ii][0];
        }
    }
   
    return "0B"
}

Coode Software Team