I'm using this function to convert a file size in bytes to a human-readable file size:
(我正在使用此函数将文件大小(以字节为单位)转换为人类可读的文件大小:)
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];
};
However, it seems like this isn't 100% accurate.
(但是,似乎这并非100%准确。)
For example:(例如:)
getReadableFileSizeString(1551859712); // output is "1.4 GB"
Shouldn't this be "1.5 GB"
?
(这不应该是"1.5 GB"
吗?)
It seems like the division by 1024 is losing precision.(似乎1024的划分正在失去精度。)
Am I totally misunderstanding something or is there a better way to do this?(我完全误解了某些东西,还是有更好的方法来做到这一点?)
ask by Hristo translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…