style: Update formatFileSize function to accept decimals parameter

This commit is contained in:
jxxghp
2024-08-12 08:18:15 +08:00
parent 676ff8789b
commit 809bfbb42a
3 changed files with 45 additions and 6 deletions

View File

@@ -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]}`
}
// 将时间秒格式化为时分秒