From 809bfbb42a91af4e315c07fc980df91cf2c2cef6 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 12 Aug 2024 08:18:15 +0800 Subject: [PATCH] style: Update formatFileSize function to accept decimals parameter --- src/@core/utils/formatters.ts | 4 +-- src/components/cards/DownloaderCard.vue | 45 +++++++++++++++++++++++-- src/styles/styles.scss | 2 +- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/@core/utils/formatters.ts b/src/@core/utils/formatters.ts index 8a3101c5..1b54972c 100644 --- a/src/@core/utils/formatters.ts +++ b/src/@core/utils/formatters.ts @@ -60,7 +60,7 @@ export const prefixWithPlus = (value: number) => (value > 0 ? `+${value}` : valu export const formatSeason = (value: string) => (value ? `S${value.padStart(2, '0')}` : '') // 格式化为xx[TGMK]B -export function formatFileSize(bytes: number) { +export function formatFileSize(bytes: number, decimals = 2) { if (bytes < 0) throw new Error('字节数不能为负数。') const units = ['B', 'KB', 'MB', 'GB', 'TB'] @@ -72,7 +72,7 @@ export function formatFileSize(bytes: number) { unitIndex++ } - return `${size.toFixed(2)} ${units[unitIndex]}` + return `${size.toFixed(decimals)} ${units[unitIndex]}` } // 将时间秒格式化为时分秒 diff --git a/src/components/cards/DownloaderCard.vue b/src/components/cards/DownloaderCard.vue index 2020b48b..cebe3a7c 100644 --- a/src/components/cards/DownloaderCard.vue +++ b/src/components/cards/DownloaderCard.vue @@ -1,6 +1,8 @@