BinaryByteSize.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Globalization;
  3. namespace Optimizer {
  4. public partial struct ByteSize {
  5. public const long BytesInKibiByte = 1_024;
  6. public const long BytesInMebiByte = 1_048_576;
  7. public const long BytesInGibiByte = 1_073_741_824;
  8. public const long BytesInTebiByte = 1_099_511_627_776;
  9. public const long BytesInPebiByte = 1_125_899_906_842_624;
  10. public const string KibiByteSymbol = "KiB";
  11. public const string MebiByteSymbol = "MiB";
  12. public const string GibiByteSymbol = "GiB";
  13. public const string TebiByteSymbol = "TiB";
  14. public const string PebiByteSymbol = "PiB";
  15. public double KibiBytes => Bytes / BytesInKibiByte;
  16. public double MebiBytes => Bytes / BytesInMebiByte;
  17. public double GibiBytes => Bytes / BytesInGibiByte;
  18. public double TebiBytes => Bytes / BytesInTebiByte;
  19. public double PebiBytes => Bytes / BytesInPebiByte;
  20. public static ByteSize FromKibiBytes(double value) {
  21. return new ByteSize(value * BytesInKibiByte);
  22. }
  23. public static ByteSize FromMebiBytes(double value) {
  24. return new ByteSize(value * BytesInMebiByte);
  25. }
  26. public static ByteSize FromGibiBytes(double value) {
  27. return new ByteSize(value * BytesInGibiByte);
  28. }
  29. public static ByteSize FromTebiBytes(double value) {
  30. return new ByteSize(value * BytesInTebiByte);
  31. }
  32. public static ByteSize FromPebiBytes(double value) {
  33. return new ByteSize(value * BytesInPebiByte);
  34. }
  35. public ByteSize AddKibiBytes(double value) {
  36. return this + ByteSize.FromKibiBytes(value);
  37. }
  38. public ByteSize AddMebiBytes(double value) {
  39. return this + ByteSize.FromMebiBytes(value);
  40. }
  41. public ByteSize AddGibiBytes(double value) {
  42. return this + ByteSize.FromGibiBytes(value);
  43. }
  44. public ByteSize AddTebiBytes(double value) {
  45. return this + ByteSize.FromTebiBytes(value);
  46. }
  47. public ByteSize AddPebiBytes(double value) {
  48. return this + ByteSize.FromPebiBytes(value);
  49. }
  50. public string ToBinaryString() {
  51. return this.ToString("0.##", CultureInfo.CurrentCulture, useBinaryByte: true);
  52. }
  53. public string ToBinaryString(IFormatProvider formatProvider) {
  54. return this.ToString("0.##", formatProvider, useBinaryByte: true);
  55. }
  56. }
  57. }