Converting file size in bytes to human-readable string

后端 未结 19 2123
眼角桃花
眼角桃花 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:36

    sizeOf = function (bytes) {
      if (bytes == 0) { return "0.00 B"; }
      var e = Math.floor(Math.log(bytes) / Math.log(1024));
      return (bytes/Math.pow(1024, e)).toFixed(2)+' '+' KMGTP'.charAt(e)+'B';
    }
    

    sizeOf(2054110009);
    //=> "1.91 GB"

    sizeOf(7054110);
    //=> "6.73 MB"

    sizeOf( (3*1024*1024) );
    //=> "3.00 MB"

提交回复
热议问题