formatFileSize

Format a byte count into a human-readable size string such as KB, MB, or GB, ideal for upload and download user interfaces.

Usage

formatFileSize formats a byte count into a human-readable file size string.

import { formatFileSize } from '@movk/core'

console.log(formatFileSize(1024)) // '1 KB'
console.log(formatFileSize(1536)) // '1.5 KB'
console.log(formatFileSize(1048576)) // '1 MB'
console.log(formatFileSize(1073741824)) // '1 GB'

// Edge cases
console.log(formatFileSize(0)) // '0 Bytes'
console.log(formatFileSize(-100)) // '0 Bytes'

API

formatFileSize(bytes)

Format a file size.

Parameters

bytes
number required
The file size in bytes.

Returns

returns
string
The formatted file size string.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License