Converting file size in bytes to human-readable string

后端 未结 19 2124
眼角桃花
眼角桃花 2020-11-28 17:58

I\'m using this function to convert a file size in bytes to a human-readable file size:

function getReadableFileSizeString(fileSizeInBytes) {
    var i = -1;         


        
19条回答
  •  孤独总比滥情好
    2020-11-28 18:26

    Another example similar to those here

    function fileSize(b) {
        var u = 0, s=1024;
        while (b >= s || -b >= s) {
            b /= s;
            u++;
        }
        return (u ? b.toFixed(1) + ' ' : b) + ' KMGTPEZY'[u] + 'B';
    }
    

    It measures negligibly better performance than the others with similar features.

提交回复
热议问题