const prettyBytes = (num: number, precision: number = 3, addSpace: boolean = true) => {
        const UNITS: Array<string> = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0];
        const exponent: number = Math.min(
          Math.floor(Math.log10(num < 0 ? -num : num) / 3),
          UNITS.length - 1
        );
        const n: number = Number(
          ((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision)
        );
        return (num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent];
      };
      

      Typescript language logo
      Bytes to human string

      Codiga's TypeScript Recipes

      Converts a number in bytes to a human-readable string.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.