From f1fefdf1a474630c054c0bd8f70024cebd138de2 Mon Sep 17 00:00:00 2001 From: bourquecharles Date: Wed, 28 Apr 2021 17:58:20 -0400 Subject: [PATCH] CleanBytesString now deals with decimals --- src/utils/helper.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/helper.js b/src/utils/helper.js index 6abc371..7dd4a67 100644 --- a/src/utils/helper.js +++ b/src/utils/helper.js @@ -2,11 +2,13 @@ export const cleanTimestamp = (timestamp) => { return timestamp.replace('T', ' ').replace('Z', ' '); } -export const cleanBytesString = (bytes) => { +export const cleanBytesString = (bytes, decimals = 2) => { const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; if (!bytes || bytes === 0) { return '0 B'; } - const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); - return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; + const k = 1024; + 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]; } \ No newline at end of file