EUS/utilities/FormattingUtility.ts

20 lines
No EOL
584 B
TypeScript

const SPACE_VALUES = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
export default class FormattingUtility {
public static NumberHumanReadable(num: number) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
// HSNOTE: Assumes bytes!
public static NumberAsFileSize(num: number) {
// Converts space values to lower values e.g MB, GB, TB etc depending on the size of the number
let i = 1;
// Loop through until value is at it's lowest
while (num >= 1024) {
num = num / 1024;
if (num >= 1024) i++;
}
return `${num.toFixed(2)} ${SPACE_VALUES[i]}`;
}
}