/// <summary> /// Creates the quantity with the given numeric value and unit. /// </summary> /// <param name="value">The numeric value to construct this quantity with.</param> /// <param name="unit">The unit representation to construct this quantity with.</param> /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks> /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception> private ReciprocalArea(double value, ReciprocalAreaUnit unit) { if (unit == ReciprocalAreaUnit.Undefined) { throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); } _value = Guard.EnsureValidNumber(value, nameof(value)); _unit = unit; }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(ReciprocalAreaUnit unit) { if (Unit == unit) { return(Convert.ToDouble(Value)); } var converted = AsBaseNumericType(unit); return(Convert.ToDouble(converted)); }
// ReSharper restore VirtualMemberNeverOverriden.Global protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReciprocalAreaUnit unit) { return(unit switch { ReciprocalAreaUnit.InverseSquareCentimeter => (InverseSquareCentimetersInOneInverseSquareMeter, InverseSquareCentimetersTolerance), ReciprocalAreaUnit.InverseSquareDecimeter => (InverseSquareDecimetersInOneInverseSquareMeter, InverseSquareDecimetersTolerance), ReciprocalAreaUnit.InverseSquareFoot => (InverseSquareFeetInOneInverseSquareMeter, InverseSquareFeetTolerance), ReciprocalAreaUnit.InverseSquareInch => (InverseSquareInchesInOneInverseSquareMeter, InverseSquareInchesTolerance), ReciprocalAreaUnit.InverseSquareKilometer => (InverseSquareKilometersInOneInverseSquareMeter, InverseSquareKilometersTolerance), ReciprocalAreaUnit.InverseSquareMeter => (InverseSquareMetersInOneInverseSquareMeter, InverseSquareMetersTolerance), ReciprocalAreaUnit.InverseSquareMicrometer => (InverseSquareMicrometersInOneInverseSquareMeter, InverseSquareMicrometersTolerance), ReciprocalAreaUnit.InverseSquareMile => (InverseSquareMilesInOneInverseSquareMeter, InverseSquareMilesTolerance), ReciprocalAreaUnit.InverseSquareMillimeter => (InverseSquareMillimetersInOneInverseSquareMeter, InverseSquareMillimetersTolerance), ReciprocalAreaUnit.InverseSquareYard => (InverseSquareYardsInOneInverseSquareMeter, InverseSquareYardsTolerance), ReciprocalAreaUnit.InverseUsSurveySquareFoot => (InverseUsSurveySquareFeetInOneInverseSquareMeter, InverseUsSurveySquareFeetTolerance), _ => throw new NotSupportedException() });
private double GetValueAs(ReciprocalAreaUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = GetValueInBaseUnit(); switch (unit) { case ReciprocalAreaUnit.InverseSquareCentimeter: return(baseUnitValue * 1e-4); case ReciprocalAreaUnit.InverseSquareDecimeter: return(baseUnitValue * 1e-2); case ReciprocalAreaUnit.InverseSquareFoot: return(baseUnitValue * 0.092903); case ReciprocalAreaUnit.InverseSquareInch: return(baseUnitValue * 0.00064516); case ReciprocalAreaUnit.InverseSquareKilometer: return(baseUnitValue * 1e6); case ReciprocalAreaUnit.InverseSquareMeter: return(baseUnitValue); case ReciprocalAreaUnit.InverseSquareMicrometer: return(baseUnitValue * 1e-12); case ReciprocalAreaUnit.InverseSquareMile: return(baseUnitValue * 2.59e6); case ReciprocalAreaUnit.InverseSquareMillimeter: return(baseUnitValue * 1e-6); case ReciprocalAreaUnit.InverseSquareYard: return(baseUnitValue * 0.836127); case ReciprocalAreaUnit.InverseUsSurveySquareFoot: return(baseUnitValue * 0.09290341161); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
/// <summary> /// Creates the quantity with the given numeric value and unit. /// </summary> /// <param name="value">The numeric value to construct this quantity with.</param> /// <param name="unit">The unit representation to construct this quantity with.</param> /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception> public ReciprocalArea(double value, ReciprocalAreaUnit unit) { _value = value; _unit = unit; }
/// <summary> /// Converts this Duration to another Duration with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A Duration with the specified unit.</returns> public ReciprocalArea ToUnit(ReciprocalAreaUnit unit) { var convertedValue = GetValueAs(unit); return(new ReciprocalArea(convertedValue, unit)); }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(ReciprocalAreaUnit unit) => GetValueAs(unit);
/// <summary> /// Dynamically convert from value and unit enum <see cref="ReciprocalAreaUnit" /> to <see cref="ReciprocalArea" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>ReciprocalArea unit value.</returns> public static ReciprocalArea From(double value, ReciprocalAreaUnit fromUnit) { return(new ReciprocalArea(value, fromUnit)); }
/// <summary> /// Converts this ReciprocalArea to another ReciprocalArea with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A ReciprocalArea with the specified unit.</returns> public ReciprocalArea ToUnit(ReciprocalAreaUnit unit) { var convertedValue = AsBaseNumericType(unit); return(new ReciprocalArea(convertedValue, unit)); }
/// <summary> /// Parse a unit string. /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="unit">The parsed unit if successful.</param> /// <returns>True if successful, otherwise false.</returns> /// <example> /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// </example> /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param> public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ReciprocalAreaUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitParser.Default.TryParse <ReciprocalAreaUnit>(str, provider, out unit)); }
public static bool TryParseUnit(string str, out ReciprocalAreaUnit unit) { return(TryParseUnit(str, null, out unit)); }
/// <summary> /// Get unit abbreviation string. /// </summary> /// <param name="unit">Unit to get abbreviation for.</param> /// <returns>Unit abbreviation string.</returns> /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param> public static string GetAbbreviation(ReciprocalAreaUnit unit, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider)); }
/// <summary> /// Get unit abbreviation string. /// </summary> /// <param name="unit">Unit to get abbreviation for.</param> /// <returns>Unit abbreviation string.</returns> public static string GetAbbreviation(ReciprocalAreaUnit unit) { return(GetAbbreviation(unit, null)); }