Files
convert/frontend/src/utils/formatBytes.js
T

11 lines
433 B
JavaScript

const UNITS = ['B', 'KB', 'MB', 'GB'];
export function formatBytes(bytes) {
if (!bytes || bytes <= 0) return '0 B';
const exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), UNITS.length - 1);
const value = bytes / 1024 ** exponent;
const rounded = Math.round(value * 10) / 10;
const formatted = Number.isInteger(rounded) ? String(rounded) : rounded.toFixed(1);
return `${formatted} ${UNITS[exponent]}`;
}