private static bool TryParseUnit(string unit, out FileLengthUnit result) { var last = unit[unit.Length - 1]; if (last != 'b' && last != 'B') { result = default; return(false); } if (unit.Length > 1) { var multiple = unit.Substring(0, unit.Length - 1); switch (multiple.ToUpperInvariant()) { case "K": result = FileLengthUnit.KiloByte; return(true); case "M": result = FileLengthUnit.MegaByte; return(true); case "G": result = FileLengthUnit.GigaByte; return(true); case "T": result = FileLengthUnit.TeraByte; return(true); case "P": result = FileLengthUnit.PetaByte; return(true); case "KI": result = FileLengthUnit.KibiByte; return(true); case "MI": result = FileLengthUnit.MebiByte; return(true); case "GI": result = FileLengthUnit.GibiByte; return(true); case "TI": result = FileLengthUnit.TebiByte; return(true); case "PI": result = FileLengthUnit.PebiByte; return(true); } } result = FileLengthUnit.Byte; return(true); }
private static string UnitToString(FileLengthUnit unit) { return(unit switch { FileLengthUnit.Byte => "B", FileLengthUnit.KiloByte => "kB", FileLengthUnit.MegaByte => "MB", FileLengthUnit.GigaByte => "GB", FileLengthUnit.TeraByte => "TB", FileLengthUnit.PetaByte => "PB", FileLengthUnit.ExaByte => "EB", FileLengthUnit.KibiByte => "kiB", FileLengthUnit.MebiByte => "MiB", FileLengthUnit.GibiByte => "GiB", FileLengthUnit.TebiByte => "TiB", FileLengthUnit.PebiByte => "PiB", FileLengthUnit.ExbiByte => "EiB", _ => throw new ArgumentOutOfRangeException(nameof(unit)), });
public double GetLength(FileLengthUnit unit) { return((double)Length / (long)unit); }