BinaryByteSize.cs 2.6 KB

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