DecimalByteSize.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Globalization;
  3. namespace Optimizer
  4. {
  5. public partial struct ByteSize
  6. {
  7. public const long BytesInKiloByte = 1_000;
  8. public const long BytesInMegaByte = 1_000_000;
  9. public const long BytesInGigaByte = 1_000_000_000;
  10. public const long BytesInTeraByte = 1_000_000_000_000;
  11. public const long BytesInPetaByte = 1_000_000_000_000_000;
  12. public const string KiloByteSymbol = "KB";
  13. public const string MegaByteSymbol = "MB";
  14. public const string GigaByteSymbol = "GB";
  15. public const string TeraByteSymbol = "TB";
  16. public const string PetaByteSymbol = "PB";
  17. public double KiloBytes => Bytes / BytesInKiloByte;
  18. public double MegaBytes => Bytes / BytesInMegaByte;
  19. public double GigaBytes => Bytes / BytesInGigaByte;
  20. public double TeraBytes => Bytes / BytesInTeraByte;
  21. public double PetaBytes => Bytes / BytesInPetaByte;
  22. public static ByteSize FromKiloBytes(double value)
  23. {
  24. return new ByteSize(value * BytesInKiloByte);
  25. }
  26. public static ByteSize FromMegaBytes(double value)
  27. {
  28. return new ByteSize(value * BytesInMegaByte);
  29. }
  30. public static ByteSize FromGigaBytes(double value)
  31. {
  32. return new ByteSize(value * BytesInGigaByte);
  33. }
  34. public static ByteSize FromTeraBytes(double value)
  35. {
  36. return new ByteSize(value * BytesInTeraByte);
  37. }
  38. public static ByteSize FromPetaBytes(double value)
  39. {
  40. return new ByteSize(value * BytesInPetaByte);
  41. }
  42. public ByteSize AddKiloBytes(double value)
  43. {
  44. return this + ByteSize.FromKiloBytes(value);
  45. }
  46. public ByteSize AddMegaBytes(double value)
  47. {
  48. return this + ByteSize.FromMegaBytes(value);
  49. }
  50. public ByteSize AddGigaBytes(double value)
  51. {
  52. return this + ByteSize.FromGigaBytes(value);
  53. }
  54. public ByteSize AddTeraBytes(double value)
  55. {
  56. return this + ByteSize.FromTeraBytes(value);
  57. }
  58. public ByteSize AddPetaBytes(double value)
  59. {
  60. return this + ByteSize.FromPetaBytes(value);
  61. }
  62. }
  63. }