formatBytesBase10.tsx 795 B

12345678910111213141516171819202122232425
  1. import {formatNumberWithDynamicDecimalPoints} from '../number/formatNumberWithDynamicDecimalPoints';
  2. /**
  3. * Note the difference between *a-bytes (base 10) vs *i-bytes (base 2), which
  4. * means that:
  5. * - 1000 megabytes is equal to 1 gigabyte
  6. * - 1024 mebibytes is equal to 1 gibibytes
  7. *
  8. * We will use base 10 throughout billing for attachments. This function formats
  9. * quota/usage values for display.
  10. *
  11. * For storage/memory/file sizes, please take a look at formatBytesBase2
  12. */
  13. export function formatBytesBase10(bytes: number, u: number = 0) {
  14. const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  15. const threshold = 1000;
  16. while (bytes >= threshold) {
  17. bytes /= threshold;
  18. u += 1;
  19. }
  20. return formatNumberWithDynamicDecimalPoints(bytes) + ' ' + units[u];
  21. }