From 8a1e7674df37ab18924dd189d8b450dfb5357ee2 Mon Sep 17 00:00:00 2001 From: Jesse Hu Date: Thu, 18 May 2023 12:01:02 +0800 Subject: [PATCH] feat: show precise file size with decimal (#210) --- assets/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/assets/index.js b/assets/index.js index 7b99784..54c3ed4 100644 --- a/assets/index.js +++ b/assets/index.js @@ -778,7 +778,11 @@ function formatSize(size) { const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; if (size == 0) return [0, "B"]; const i = parseInt(Math.floor(Math.log(size) / Math.log(1024))); - return [Math.round(size / Math.pow(1024, i), 2), sizes[i]]; + ratio = 1 + if (i >= 3) { + ratio = 100 + } + return [Math.round(size * ratio / Math.pow(1024, i), 2) / ratio, sizes[i]]; } function formatDuration(seconds) { @@ -818,4 +822,4 @@ function getEncoding(contentType) { } } return 'utf-8' -} \ No newline at end of file +}