💄 Human readable filesize format pipe
This commit is contained in:
parent
ea1e21042b
commit
4db490eea4
12
src/src/app/shared/filesize.pipe.ts
Normal file
12
src/src/app/shared/filesize.pipe.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({ name: 'fileSize', standalone: true })
|
||||
export class FileSizePipe implements PipeTransform {
|
||||
transform(bytes: number, digits: number = 2): string {
|
||||
if (isNaN(bytes) || bytes === 0) return '0 B';
|
||||
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
return `${parseFloat((bytes / Math.pow(1024, i)).toFixed(digits))} ${sizes[i]}`;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user