Function: prettyBytes

prettyBytes(num, precision?, addSpace?): string

将字节数转换为易读的格式(如 KB、MB、GB)

Parameters

Name Type Default value Description
num number undefined 要转换的字节数(可正可负)
precision? number 3 显示精度(有效数字位数),默认为3
addSpace? boolean true 是否在数字和单位间添加空格,默认为true

Returns

string

格式化后的字符串

Description

将字节数转换为带单位的友好显示格式,支持自定义精度和间距

Throws

当输入参数不是数字时抛出

Example

prettyBytes(10000); // => '1 KB'
prettyBytes(-27145424323.5821, 5); // => '-27.145 GB'
prettyBytes(123456789, 3, false); // => '123MB'
ON THIS PAGE