Converting file size in bytes to human-readable string

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

    1551859712 / 1024 = 1515488
    1515488 / 1024 = 1479.96875
    1479.96875 / 1024 = 1.44528198242188
    

    Your solution is correct. The important thing to realize is that in order to get from 1551859712 to 1.5, you have to do divisions by 1000, but bytes are counted in binary-to-decimal chunks of 1024, hence why the Gigabyte value is less.

提交回复
热议问题