[O] Better sizeFmt

This commit is contained in:
Azalea Gui
2023-02-14 14:30:59 -05:00
parent c70325c3b7
commit b2060fe96a
+4 -2
View File
@@ -5,8 +5,10 @@
* @returns Human readable size in string * @returns Human readable size in string
*/ */
export function sizeFmt(size: number): string { export function sizeFmt(size: number): string {
var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)); const i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(1) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; size = size / Math.pow(1024, i)
const fmt = size >= 100 ? size.toFixed(0) : size.toFixed(1)
return fmt + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
} }
export function clamp(num: number, min: number, max: number) { export function clamp(num: number, min: number, max: number) {