TextFormatter.cs 787 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace QVCopier.Utility
  7. {
  8. public static class TextFormatter
  9. {
  10. public static string ToFriendFileSize(this long value)
  11. {
  12. string[] sizes = { "B", "KB", "MB", "GB", "TB" };
  13. double len = value;
  14. var order = 0;
  15. while (len >= 1024 && order < sizes.Length - 1)
  16. {
  17. order++;
  18. len = len / 1024;
  19. }
  20. // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
  21. // show a single decimal place, and no space.
  22. var result = $"{len:0.##} {sizes[order]}";
  23. return result;
  24. }
  25. }
  26. }