/// <summary> /// Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm". /// This is particularly useful for creating things like a generated unit conversion UI, /// where you list some selectors: /// a) Quantity: Length, Mass, Force etc. /// b) From unit: Meter, Centimeter etc if Length is selected /// c) To unit: Meter, Centimeter etc if Length is selected /// </summary> /// <param name="fromValue"> /// Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to /// convert from. /// </param> /// <param name="quantityName"> /// Name of quantity, such as "Length" and "Mass". <see cref="QuantityType" /> for all /// values. /// </param> /// <param name="fromUnitAbbrev"> /// Name of unit, such as "Meter" or "Centimeter" if "Length" was passed as /// <paramref name="quantityName" />. /// </param> /// <param name="toUnitAbbrev"> /// Name of unit, such as "Meter" or "Centimeter" if "Length" was passed as /// <paramref name="quantityName" />. /// </param> /// <param name="culture">Culture to parse abbreviations with.</param> /// <param name="result">Result if conversion was successful, 0 if not.</param> /// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example> /// <returns>True if conversion was successful.</returns> public static bool TryConvertByAbbreviation(QuantityValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, out double result, string culture) { result = 0d; if (!TryGetUnitType(quantityName, out Type unitType)) { return(false); } var cultureInfo = string.IsNullOrWhiteSpace(culture) ? CultureInfo.CurrentUICulture : new CultureInfo(culture); if (!UnitParser.Default.TryParse(fromUnitAbbrev, unitType, cultureInfo, out Enum fromUnit)) // ex: ("m", LengthUnit) => LengthUnit.Meter { return(false); } if (!UnitParser.Default.TryParse(toUnitAbbrev, unitType, cultureInfo, out Enum toUnit)) // ex:("cm", LengthUnit) => LengthUnit.Centimeter { return(false); } var fromQuantity = Quantity.From(fromValue, fromUnit); result = fromQuantity.As(toUnit); return(true); }
/// <summary> /// Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm". /// This is particularly useful for creating things like a generated unit conversion UI, /// where you list some selectors: /// a) Quantity: Length, Mass, Force etc. /// b) From unit: Meter, Centimeter etc if Length is selected /// c) To unit: Meter, Centimeter etc if Length is selected /// </summary> /// <param name="fromValue"> /// Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to /// convert from. /// </param> /// <param name="quantityName"> /// Name of quantity, such as "Length" and "Mass". <see cref="QuantityType" /> for all /// values. /// </param> /// <param name="fromUnitAbbrev"> /// Name of unit, such as "Meter" or "Centimeter" if "Length" was passed as /// <paramref name="quantityName" />. /// </param> /// <param name="toUnitAbbrev"> /// Name of unit, such as "Meter" or "Centimeter" if "Length" was passed as /// <paramref name="quantityName" />. /// </param> /// <param name="culture">Culture to parse abbreviations with.</param> /// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example> /// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns> /// <exception cref="UnitNotFoundException"> /// No unit types match the prefix of <paramref name="quantityName" /> or no units /// are mapped to the abbreviation. /// </exception> /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation.</exception> public static double ConvertByAbbreviation(QuantityValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, string culture) { if (!TryGetUnitType(quantityName, out Type unitType)) { throw new UnitNotFoundException($"The unit type for the given quantity was not found: {quantityName}"); } var cultureInfo = string.IsNullOrWhiteSpace(culture) ? CultureInfo.CurrentUICulture : new CultureInfo(culture); var fromUnit = UnitParser.Default.Parse(fromUnitAbbrev, unitType, cultureInfo); // ex: ("m", LengthUnit) => LengthUnit.Meter var fromQuantity = Quantity.From(fromValue, fromUnit); var toUnit = UnitParser.Default.Parse(toUnitAbbrev, unitType, cultureInfo); // ex:("cm", LengthUnit) => LengthUnit.Centimeter return(fromQuantity.As(toUnit)); }
/// <summary>Computes the min of a sequence of <typeparamref name="TQuantity" /> values.</summary> /// <param name="source">A sequence of <typeparamref name="TQuantity" /> values to calculate the min of.</param> /// <param name="unitType">The desired unit type for the resulting quantity</param> /// <returns>The min of the values in the sequence, represented in the specified unit type.</returns> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="source">source</paramref> is null. /// </exception> /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> /// <exception cref="ArgumentException"> /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. /// </exception> public static TQuantity Min <TQuantity>(this IEnumerable <TQuantity> source, Enum unitType) where TQuantity : IQuantity { return((TQuantity)Quantity.From(source.Min(x => x.As(unitType)), unitType)); }
/// <summary> /// Convert between any two quantity units given a numeric value and two unit enum values. /// </summary> /// <param name="fromValue">Numeric value.</param> /// <param name="fromUnitValue">From unit enum value.</param> /// <param name="toUnitValue">To unit enum value, must be compatible with <paramref name="fromUnitValue" />.</param> /// <returns>The converted value in the new unit representation.</returns> public static double Convert(QuantityValue fromValue, Enum fromUnitValue, Enum toUnitValue) { return(Quantity .From(fromValue, fromUnitValue) .As(toUnitValue)); }
/// <summary>Returns the absolute value of a <typeparamref name="TQuantity" />.</summary> /// <param name="value"> /// A quantity with a value that is greater than or equal to <see cref="F:System.Double.MinValue" />, /// but less than or equal to <see cref="F:System.Double.MaxValue" />. /// </param> /// <returns>A quantity with a value, such that 0 ≤ value ≤ <see cref="F:System.Double.MaxValue" />.</returns> public static TQuantity Abs <TQuantity>(this TQuantity value) where TQuantity : IQuantity { return(value.Value >= 0 ? value : (TQuantity)Quantity.From(-value.Value, value.Unit)); }