Appearance
将字节转换成数字
Hidden Title
基础用法:
formatBytesConvert('1GB')基础用法
说明
formatBytesConvert 用于把带单位的字节文本转换成原始字节数,例如把 1.5GB 转成 1610612736。它也可以处理纯数字、数字字符串和带千分位的输入。
调用形式
ts
formatBytesConvert(value)
formatBytesConvert(value, options)formatBytesConvert(value)
formatBytesConvert(value, options)参数说明
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
value | unknown | 是 | - | 要转换的值。支持 100GB、1,024 KB、数字、数字字符串等。 |
options | FormatBytesConvertOptions | 否 | {} | 控制转换后展示格式的配置项。 |
FormatBytesConvertOptions 字段:
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
digit | number | 0 | 转换结果需要保留的小数位数。大于 0 时返回字符串。 |
thounsands | boolean | false | 是否给转换结果添加千分位分隔符。字段名按源码保持为 thounsands。 |
返回值
返回字节数。默认通常返回 number;启用 digit 或 thounsands 后会返回格式化后的 string。输入格式非法时返回 undefined 并在控制台输出警告。
常用场景
ts
formatBytesConvert('1GB')
// 1073741824
formatBytesConvert('1,024 KB')
// 1048576
formatBytesConvert('1.5GB', { digit: 2 })
// '1610612736.00'
formatBytesConvert(1040000, { thounsands: true })
// '1,040,000'formatBytesConvert('1GB')
// 1073741824
formatBytesConvert('1,024 KB')
// 1048576
formatBytesConvert('1.5GB', { digit: 2 })
// '1610612736.00'
formatBytesConvert(1040000, { thounsands: true })
// '1,040,000'注意事项
支持的单位包括 B、Byte、KB、MB、GB、TB、PB、EB、ZB、YB,单位大小写不敏感。thounsands 是当前 API 的实际字段名,使用时不要写成 thousands。
函数源码
formatBytesConvert
来源:packages/utils/src/format.ts
/**
* 将带单位的字节字符串转换为原始字节数。
*
* @param oBytes 字节字符串,例如 `1.5GB`、`1,024 KB`,也支持纯数字。
* @param options 配置项。
* @returns 字节数;如启用了 `digit` 或 `thounsands`,返回格式化后的字符串。
*
* @example
* formatBytesConvert('0.5GB')
* // => 536870912
*
* @example
* formatBytesConvert('1,234 GB', { thounsands: true })
* // => '1,324,997,410,816'
*/
export function formatBytesConvert(
oBytes: unknown,
options: FormatBytesConvertOptions = {},
): string | number | undefined {
let { thounsands = false, digit = 0 } = options
let bytes = oBytes
const parseDigitThounsands = (val: unknown) => {
let finalRes = val as any
if (digit) {
finalRes = Number(finalRes).toFixed(digit)
}
if (thounsands) {
finalRes = formatThousands(finalRes)
}
return finalRes
}
if (isStringNumber(oBytes as string) || isNumber(oBytes) || getType(oBytes) !== 'string') {
return parseDigitThounsands(oBytes)
}
if (!oBytes) {
return parseDigitThounsands(oBytes)
}
const regex = /^\d{1,3}(,\d{3})*(\.\d+)?[a-zA-Z ]*$/
if (typeof oBytes === 'string' && regex.test(oBytes)) {
const normalizedBytes = oBytes.replace(/,/g, '')
bytes = normalizedBytes
if (isStringNumber(normalizedBytes) || isNumber(normalizedBytes) || getType(normalizedBytes) !== 'string') {
return parseDigitThounsands(normalizedBytes)
}
}
const bytesRegex = /^(\d+(?:\.\d+)?)\s*([BKMGTPEZY]?B|Byte)$/i
const units = {
B: 1,
BYTE: 1,
KB: 1024,
MB: 1024 ** 2,
GB: 1024 ** 3,
TB: 1024 ** 4,
PB: 1024 ** 5,
EB: 1024 ** 6,
ZB: 1024 ** 7,
YB: 1024 ** 8,
}
if (typeof bytes !== 'string') {
return parseDigitThounsands(bytes)
}
const match = bytes.match(bytesRegex)
if (!match) {
consola.warn("Invalid bytes format. Please provide a valid bytes string, like '100GB'.")
return
}
const size = parseFloat(match[1])
const unit = match[2].toUpperCase() as keyof typeof units
if (!Object.prototype.hasOwnProperty.call(units, unit)) {
consola.warn(
"Invalid bytes unit. Please provide a valid unit, like 'B', 'BYTE', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', or 'YB'.",
)
return
}
return parseDigitThounsands(size * units[unit])
}