machines/projects/Printhead/firmware/node/src/formatter.ts
2023-11-12 21:43:05 +01:00

18 lines
462 B
TypeScript

export const sizeToString = (bytes: number, si: boolean = true) => {
var units;
var u;
var b = bytes;
var thresh = si ? 1000 : 1024;
if (Math.abs(b) < thresh) {
return b + ' B';
}
units = si
? ['kB', 'MB', 'GB', 'TB']
: ['KiB', 'MiB', 'GiB', 'TiB'];
u = -1;
do {
b /= thresh;
++u;
} while (Math.abs(b) >= thresh && u < units.length - 1);
return b.toFixed(1) + ' ' + units[u];
};