UNPKG

@iicoding/utils

Version:

Browser 端 - 类型判断 - 类似 koa 的异步compose - sleep - 扩展对象属性 - 扩展 storage 对象功能

17 lines 582 B
/** * 转换GB TB 等大小 * @param bytes 字节数 * @param decimals 保留小数位数 */ export function byteConvert(bytes) { var decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; if (Number.isNaN(bytes)) { return ''; } if (bytes === 0) return '0 Bytes'; var k = 1024; var dm = decimals < 0 ? 0 : decimals; var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; var i = Math.floor(Math.log(bytes) / Math.log(k)); return "".concat(parseFloat((bytes / Math.pow(k, i)).toFixed(dm)), " ").concat(sizes[i]); }