• Converts a given number of bytes into a human-readable string with the appropriate unit.

    Parameters

    • bytes: number

      The number of bytes to be converted.

    • decimals: number = 2

      The number of decimal places to include in the formatted string. Defaults to 2.

    Returns
        | `${number} Bytes`
        | `${number} KiB`
        | `${number} MiB`
        | `${number} GiB`
        | `${number} TiB`
        | `${number} PiB`
        | `${number} EiB`
        | `${number} ZiB`
        | `${number} YiB`

    A string representing the formatted number of bytes with the appropriate unit.

    https://stackoverflow.com/a/18650828 The souce of the function (It was originally in JavaScript but I converted it to TypeScript.).

    formatBytes(1024); // "1 KiB"
    formatBytes(123456789); // "117.74 MiB"
    formatBytes(123456789, 3); // "117.738 MiB"