Long2FileSizeConverter.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Globalization;
  3. using System.Windows.Data;
  4. namespace HandyControl.Tools.Converter;
  5. public class Long2FileSizeConverter : IValueConverter
  6. {
  7. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  8. {
  9. if (value == null) return Properties.Langs.Lang.UnknownSize;
  10. if (value is long intValue)
  11. {
  12. if (intValue < 0)
  13. {
  14. return Properties.Langs.Lang.UnknownSize;
  15. }
  16. if (intValue < 1024)
  17. {
  18. return $"{intValue} B";
  19. }
  20. if (intValue < 1048576)
  21. {
  22. return $"{intValue / 1024.0:0.00} KB";
  23. }
  24. if (intValue < 1073741824)
  25. {
  26. return $"{intValue / 1048576.0:0.00} MB";
  27. }
  28. if (intValue < 1099511627776)
  29. {
  30. return $"{intValue / 1073741824.0:0.00} GB";
  31. }
  32. if (intValue < 1125899906842624)
  33. {
  34. return $"{intValue / 1099511627776.0:0.00} TB";
  35. }
  36. if (intValue < 1152921504606847000)
  37. {
  38. return $"{intValue / 1125899906842624.0:0.00} PB";
  39. }
  40. return Properties.Langs.Lang.TooLarge;
  41. }
  42. return Properties.Langs.Lang.Unknown;
  43. }
  44. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  45. {
  46. throw new NotSupportedException();
  47. }
  48. }