我使用此函数将以字节为单位的文件大小转换为人类可读的文件大小:
function getReadableFileSizeString(fileSizeInBytes) {
var i = -1;
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
do {
fileSizeInBytes = fileSizeInBytes / 1024;
i++;
} while (fileSizeInBytes > 1024);
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
};
然而,这似乎并不是100%准确的。例如:
getReadableFileSizeString(1551859712); // output is "1.4 GB"
这不应该是"1.5 GB"
吗?看起来1024的除法正在失去精度。是我完全误解了什么吗,还是有更好的方法呢?
https://stackoverflow.com/questions/10420352
复制相似问题