mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
added enum to use base 2 or base 10 for calcs
This commit is contained in:
1 parent
c5ba4f356f
commit
ea6b3d0c38
1 file changed
+7
-2
@@ -1,9 +1,14 @@
|
|||||||
export function formatBytes(bytes: number, decimals = 2) {
|
export enum UNIT_BASE {
|
||||||
|
BASE_2 = 1024,
|
||||||
|
BASE_10 = 1000
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatBytes(bytes: number, decimals = 2, base: UNIT_BASE = UNIT_BASE.BASE_2) {
|
||||||
if (bytes === 0) {
|
if (bytes === 0) {
|
||||||
return '0 Bytes';
|
return '0 Bytes';
|
||||||
}
|
}
|
||||||
|
|
||||||
const k = 1024;
|
const k = base;
|
||||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user