[Edit]
+
0
-
0
JavaScript - format size in bytes to human-readable (KB, MB, GB, TB, PB)
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101const SHORT_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; const FULL_UNITS = ['bytes', 'kilobytes', 'megabytes', 'gigabytes', 'terabytes', 'petabytes']; // Source: https://dirask.com/snippets/JavaScript-round-floating-point-number-with-precision-Dd53lp // const roundNumber = (value, precision = 0) => { if (precision > 0) { const power = Math.pow(10, precision); return Math.round(value * power) / power; } return Math.round(value); }; const calculateSize = (bytes, units = SHORT_UNITS) => { if (bytes === 0) { return { value: 0, unit: units[0] ?? 'B' }; } const level = Math.floor(Math.log(bytes) / Math.log(1024)); const index = Math.min(level, units.length - 1); return { value: bytes / Math.pow(1024, index), unit: units[index] ?? 'B' }; }; const renderSize = (bytes, precision = 2, units = SHORT_UNITS, spacer = '') => { const size = calculateSize(bytes, units); const value = roundNumber(size.value, precision); return value + spacer + size.unit; }; const printSize = (bytes, precision = 2, units = SHORT_UNITS, spacer = '') => { console.log(renderSize(bytes, precision, units, spacer)); }; // --------------------------------------------------------- // Practical example 1 (used pre-default SHORT_UNITS units): // --------------------------------------------------------- console.log(renderSize(512)); // 512B console.log(renderSize(1536)); // 1.5KB console.log(renderSize(1572864)); // 1.5MB console.log(renderSize(1610612736)); // 1.5GB console.log(renderSize(1649267441664)); // 1.5TB console.log(renderSize(1688849860263936)); // 1.5PB // --------------------------------------------------------- // Practical example 2 (used pre-default SHORT_UNITS units): // --------------------------------------------------------- console.log(renderSize(1024)); // 1KB console.log(renderSize(1024 * 1024)); // 1MB console.log(renderSize(1024 * 1024 * 1024)); // 1GB console.log(renderSize(1024 * 1024 * 1024 * 1024)); // 1TB console.log(renderSize(1024 * 1024 * 1024 * 1024 * 1024)); // 1PB // --------------------------------------------------------- // Practical example 3 (used pre-default SHORT_UNITS units): // --------------------------------------------------------- // precision (decimal numbers) // | // v console.log(renderSize(1234567, 0)); // 1MB console.log(renderSize(1234567, 1)); // 1.2MB console.log(renderSize(1234567, 2)); // 1.18MB console.log(renderSize(1234567, 3)); // 1.177MB console.log(renderSize(1234567, 4)); // 1.1774MB console.log(renderSize(1234567, 5)); // 1.17737MB console.log(renderSize(1234567, 6)); // 1.177375MB // --------------------------------------------------------- // Practical example 4 (used FULL_UNITS units): // --------------------------------------------------------- console.log(renderSize(1234567, 0, FULL_UNITS, ' ')); // 1MB console.log(renderSize(1234567, 1, FULL_UNITS, ' ')); // 1.2MB console.log(renderSize(1234567, 2, FULL_UNITS, ' ')); // 1.18MB console.log(renderSize(1234567, 3, FULL_UNITS, ' ')); // 1.177MB console.log(renderSize(1234567, 4, FULL_UNITS, ' ')); // 1.1774MB console.log(renderSize(1234567, 5, FULL_UNITS, ' ')); // 1.17737MB console.log(renderSize(1234567, 6, FULL_UNITS, ' ')); // 1.177375MB // --------------------------------------------------- // See also: // --------------------------------------------------- // // 1. https://dirask.com/snippets/JavaScript-parse-file-size-to-bytes-input-in-B-KB-MB-GB-TB-PB-Ddkq31
Reset