/// <summary> /// Attempts to parse the given <see cref="string"/> into a /// quantity of bytes. /// </summary> /// <param name="str"> /// The <see cref="string"/> to parse. /// </param> /// <param name="bytes"> /// The quantity of bytes parsed. /// </param> /// <param name="units"> /// The units of the bytes parsed. /// </param> /// <param name="precision"> /// The precision of the parsing. /// </param> /// <returns> /// <c>true</c> if <paramref name="str"/> was able to be parsed; /// <c>false</c> otherwise. /// </returns> public static bool TryParse(string str, out ulong bytes, out BytesUnits units, out int precision) { if (string.IsNullOrWhiteSpace(str)) { bytes = 0; precision = 0; units = BytesUnits.Bytes; return(false); } str = str.Trim(); string numberText; BytesConverterUtils.SplitNumberAndUnits(str, BytesUnits.Bytes, out numberText, out units); decimal number; if (!decimal.TryParse(numberText, out number)) { bytes = 0; precision = 0; units = BytesUnits.Bytes; return(false); } precision = DecimalUtils.GetPrecision(number); decimal tempBytes = BytesConverterUtils.ConvertToBytes(number, units); bool roundResult = DecimalUtils.TryRoundToUInt64(tempBytes, MidpointRounding.AwayFromZero, out bytes); return(roundResult); }
/// <summary> /// Converts the given byte value to a quantity using the largest possible unit /// that still leaves a quantity with a magnitude of at least one in said unit. /// </summary> /// <param name="value"> /// The value to convert. /// </param> /// <param name="bytesUnits"> /// The units into which <paramref name="value"/> was converted. /// </param> /// <returns> /// The converted quantity expressed in <paramref name="bytesUnits"/> units. /// </returns> public static decimal ConvertToBytesUnits(decimal value, out BytesUnits bytesUnits) { BytesUnits[] units = new[] { BytesUnits.Bytes, BytesUnits.Kilobytes, BytesUnits.Megabytes, BytesUnits.Gigabytes }; int index = 0; while (value > 1024 && index < (units.Count() - 1)) { value /= 1024; index++; } bytesUnits = units[index]; return(value); }
/// <summary> /// Converts the given count of bytes to the given units. /// </summary> /// <param name="bytesValue"> /// The count of bytes. /// </param> /// <param name="units"> /// The units into which to convert the count. /// </param> /// <returns> /// The number of bytes as expressed in <paramref name="units"/>. /// </returns> /// <exception cref="System.ComponentModel.InvalidEnumArgumentException"> /// <paramref name="units"/> is not a valid member of the <see cref="BytesUnits"/> /// enumeration. /// </exception> public static decimal ConvertToBytesUnit(decimal bytesValue, BytesUnits units) { switch (units) { case BytesUnits.Bytes: return(bytesValue); case BytesUnits.Kilobytes: return(Decimal.Divide(bytesValue, (decimal)1024d)); case BytesUnits.Megabytes: return(Decimal.Divide(bytesValue, (decimal)1048576d)); case BytesUnits.Gigabytes: return(Decimal.Divide(bytesValue, (decimal)1073741824d)); default: throw new InvalidEnumArgumentException("units", (int)units, typeof(BytesUnits)); } }
/// <summary> /// Attempts to split the given string argument into the number /// piece and the units piece. /// </summary> /// <param name="str"> /// The <see cref="string"/> to split. /// </param> /// <param name="defaultUnits"> /// The units to assume if no units are found in <paramref name="str"/>. /// </param> /// <param name="remainingText"> /// The part of <paramref name="str"/> that could not be parsed. /// </param> /// <param name="units"> /// The units that were parsed / used. /// </param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="str"/> is <c>null</c> /// - or - /// <paramref name="str"/> is whitespace. /// </exception> public static void SplitNumberAndUnits(string str, BytesUnits defaultUnits, out string remainingText, out BytesUnits units) { if (string.IsNullOrWhiteSpace(str)) { throw new ArgumentNullException("str"); } Debug.Assert(string.Equals(str, str.Trim(), StringComparison.Ordinal)); string unitsText; if (TryExtract(str, kilobytesSuffixes, out remainingText, out unitsText)) { units = BytesUnits.Kilobytes; return; } if (TryExtract(str, megabytesSuffixes, out remainingText, out unitsText)) { units = BytesUnits.Megabytes; return; } if (TryExtract(str, gigabytesSuffixes, out remainingText, out unitsText)) { units = BytesUnits.Gigabytes; return; } if (TryExtract(str, bytesSuffixes, out remainingText, out unitsText)) { units = BytesUnits.Bytes; return; } // At this point we don't guarantee that the rest of the string can be parsed as a number. remainingText = str; units = defaultUnits; return; }