@pengzhanbo/utils - v3.9.0
    Preparing search index...

    Function formatBytes

    • Formats a number of bytes into a human-readable string.

      Uses decimal units (1000 base, e.g. KB, MB) by default, or binary units (1024 base, e.g. KiB, MiB) when binary is true. Values below the base are returned as-is with the B unit and no decimal places.

      将字节数格式化为人类可读的字符串。

      默认使用十进制单位(以 1000 为基数,如 KB、MB);当 binary 为 true 时使用 二进制单位(以 1024 为基数,如 KiB、MiB)。小于基数的值原样返回并附带 B 单位, 不保留小数位。

      Parameters

      • bytes: number

        The number of bytes to format. 要格式化的字节数

      • options: FormatBytesOptions = {}

        Formatting options. 格式化选项

        • Optionalbinary?: boolean

          Whether to use binary units (1024 base) instead of decimal units (1000 base). Defaults to false. 是否使用二进制单位(以 1024 为基数)而非十进制单位(以 1000 为基数)。默认为 false

        • Optionalprecision?: number

          Number of decimal places to keep. Defaults to 1. 保留的小数位数。默认为 1

        • Optionalwhitespace?: boolean

          Whether to include whitespace between the number and the unit. Defaults to false. 是否保留数字与单位之间的空格。默认为 false

      Returns string

      The formatted string. 格式化后的字符串

      If bytes is negative, NaN, or Infinity. 当 bytes 为负数、NaN 或 Infinity 时抛出。

      Values at or above the base keep precision decimal places and are clamped to the largest available unit (PB / PiB).

      不小于基数的值保留 precision 位小数,并停留在最大单位(PB / PiB)。

      formatBytes(500) // => '500B'
      
      formatBytes(1572864) // => '1.6MB'
      formatBytes(1572864, { binary: true }) // => '1.5MiB'
      formatBytes(1536, { precision: 2 }) // => '1.54KB'
      formatBytes(1572864, { whitespace: true }) // => '1.6 MB'