---
title: "formatFileSize"
description: "格式化文件大小,将字节数转换为可读的文件大小字符串"
seo_title: "formatFileSize"
seo_description: "Format a byte count into a human-readable size string such as KB, MB, or GB, ideal for upload and download user interfaces."
canonical_url: "https://core.mhaibaraai.cn/docs/helpers/file/format-file-size"
---
# formatFileSize

> 格式化文件大小,将字节数转换为可读的文件大小字符串

## 用法

`formatFileSize` 函数用于格式化文件大小,将字节数转换为可读的文件大小字符串。

```ts
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'

// 处理边界情况
console.log(formatFileSize(0)) // '0 Bytes'
console.log(formatFileSize(-100)) // '0 Bytes'
```

## API

### `formatFileSize(bytes)`

格式化文件大小。

### 参数

**bytes** (`number`) *required*: 文件大小(字节)。

### 返回值

**返回值** (`string`): 格式化后的文件大小字符串。

## Changelog

See commit history for [src/helpers/file/formatFileSize.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/helpers/file/formatFileSize.ts).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
