Use absolute value of bytes when determining exponent

This commit is contained in:
Hassan Azmi
2020-08-21 14:57:36 -04:00
parent c416d87106
commit 94a26fef43

View File

@@ -13,7 +13,7 @@ function formatBytes(bytes, decimals = 2) {
const dm = decimals < 0 ? 0 : decimals;
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(Math.abs(bytes)) / Math.log(k));
// eslint-disable-next-line no-restricted-properties
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i] || ''}`;