//字节转换
function byteConvert(bytes) {
if (isNaN(bytes)) {
return '';
}
let symbols = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let exp = Math.floor(Math.log(bytes)/Math.log(2));
if (exp < 1) {
exp = 0;
}
let i = Math.floor(exp / 10);
bytes = bytes / Math.pow(2, 10 * i);
if (bytes.toString().length > bytes.toFixed(2).toString().length) {
bytes = bytes.toFixed(2);
}
return bytes + ' ' + symbols[i];
};
byteConvert(1024) -- 1 KB
博客展示了一个字节转换示例,通过byteConvert函数将1024转换为1 KB,体现了字节转换的操作,与信息技术相关。

629

被折叠的 条评论
为什么被折叠?



