mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: improve formatBytes function and display byte size in textarea
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
export function formatBytes(bytes: number, decimals = 2): string {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
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));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
|
||||
const unit = 1024;
|
||||
const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
|
||||
const exponent = Math.floor(Math.log(bytes) / Math.log(unit));
|
||||
const value = bytes / Math.pow(unit, exponent);
|
||||
|
||||
const unitLabel = value === 1 && exponent === 0 ? 'Byte' : units[exponent];
|
||||
const formattedValue = Number.isInteger(value) ? value : value.toFixed(decimals);
|
||||
|
||||
return `${formattedValue} ${unitLabel}`;
|
||||
}
|
||||
|
||||
export function formatNumber(num: number): string {
|
||||
|
||||
Reference in New Issue
Block a user