javascript中根据文件大小显示接近的单位
VIVO的蓝心大模型查出来的
//将文件大小转换为最接近的指定单位:'B', 'KB', 'MB', 'GB', 'TB'
function formatFileSize(size) {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let unitIndex = 0;
while(size >= 1024){
size /= 1024;
unitIndex++;
};
return size.toFixed(2) + units[unitIndex];
}