1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
function byteConvert(bytes: number): string { let ret = ''; const 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; } const i = Math.floor(exp / 10); ret = `${bytes / Math.pow(2, 10 * i)}`;
if (bytes.toString().length > bytes.toFixed(2).toString().length) { ret = bytes.toFixed(2); } return ret + symbols[i]; }
function unitConversion(size: string): number { const symbols = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const length = parseInt(size as string, 10); const unit = (size as string).substring(length.toString().length).toUpperCase(); const index = symbols.findIndex(i => i === unit); return length * Math.pow(2, 10 * index); }
|