CleanBytesString now deals with decimals

This commit is contained in:
bourquecharles
2021-04-28 17:58:20 -04:00
parent 8465e54adb
commit f1fefdf1a4

View File

@@ -2,11 +2,13 @@ export const cleanTimestamp = (timestamp) => {
return timestamp.replace('T', ' ').replace('Z', ' '); return timestamp.replace('T', ' ').replace('Z', ' ');
} }
export const cleanBytesString = (bytes) => { export const cleanBytesString = (bytes, decimals = 2) => {
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
if (!bytes || bytes === 0) { if (!bytes || bytes === 0) {
return '0 B'; return '0 B';
} }
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); const k = 1024;
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; const dm = decimals < 0 ? 0 : decimals;
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(k)));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
} }