Exemplo n.º 1
0
        private double GetValueAs(ReciprocalLengthUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = GetValueInBaseUnit();

            switch (unit)
            {
            case ReciprocalLengthUnit.InverseCentimeter: return(baseUnitValue / 1e2);

            case ReciprocalLengthUnit.InverseFoot: return(baseUnitValue * 0.3048);

            case ReciprocalLengthUnit.InverseInch: return(baseUnitValue * 2.54e-2);

            case ReciprocalLengthUnit.InverseMeter: return(baseUnitValue);

            case ReciprocalLengthUnit.InverseMicroinch: return(baseUnitValue * 2.54e-8);

            case ReciprocalLengthUnit.InverseMil: return(baseUnitValue * 2.54e-5);

            case ReciprocalLengthUnit.InverseMile: return(baseUnitValue * 1609.34);

            case ReciprocalLengthUnit.InverseMillimeter: return(baseUnitValue / 1e3);

            case ReciprocalLengthUnit.InverseUsSurveyFoot: return(baseUnitValue * 1200 / 3937);

            case ReciprocalLengthUnit.InverseYard: return(baseUnitValue * 0.9144);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Exemplo n.º 2
0
        /// <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 ReciprocalLength(double value, ReciprocalLengthUnit unit)
        {
            if (unit == ReciprocalLengthUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(ReciprocalLengthUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Exemplo n.º 4
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReciprocalLengthUnit unit)
        {
            return(unit switch
            {
                ReciprocalLengthUnit.InverseCentimeter => (InverseCentimetersInOneInverseMeter, InverseCentimetersTolerance),
                ReciprocalLengthUnit.InverseFoot => (InverseFeetInOneInverseMeter, InverseFeetTolerance),
                ReciprocalLengthUnit.InverseInch => (InverseInchesInOneInverseMeter, InverseInchesTolerance),
                ReciprocalLengthUnit.InverseMeter => (InverseMetersInOneInverseMeter, InverseMetersTolerance),
                ReciprocalLengthUnit.InverseMicroinch => (InverseMicroinchesInOneInverseMeter, InverseMicroinchesTolerance),
                ReciprocalLengthUnit.InverseMil => (InverseMilsInOneInverseMeter, InverseMilsTolerance),
                ReciprocalLengthUnit.InverseMile => (InverseMilesInOneInverseMeter, InverseMilesTolerance),
                ReciprocalLengthUnit.InverseMillimeter => (InverseMillimetersInOneInverseMeter, InverseMillimetersTolerance),
                ReciprocalLengthUnit.InverseUsSurveyFoot => (InverseUsSurveyFeetInOneInverseMeter, InverseUsSurveyFeetTolerance),
                ReciprocalLengthUnit.InverseYard => (InverseYardsInOneInverseMeter, InverseYardsTolerance),
                _ => throw new NotSupportedException()
            });
Exemplo n.º 5
0
 /// <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 ReciprocalLength(double value, ReciprocalLengthUnit unit)
 {
     _value = value;
     _unit  = unit;
 }
Exemplo n.º 6
0
        /// <summary>
        ///     Converts this Duration to another Duration with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Duration with the specified unit.</returns>
        public ReciprocalLength ToUnit(ReciprocalLengthUnit unit)
        {
            var convertedValue = GetValueAs(unit);

            return(new ReciprocalLength(convertedValue, unit));
        }
Exemplo n.º 7
0
 /// <summary>
 ///     Convert to the unit representation <paramref name="unit" />.
 /// </summary>
 /// <returns>Value converted to the specified unit.</returns>
 public double As(ReciprocalLengthUnit unit) => GetValueAs(unit);
Exemplo n.º 8
0
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="ReciprocalLengthUnit" /> to <see cref="ReciprocalLength" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>ReciprocalLength unit value.</returns>
 public static ReciprocalLength From(double value, ReciprocalLengthUnit fromUnit)
 {
     return(new ReciprocalLength(value, fromUnit));
 }
Exemplo n.º 9
0
        /// <summary>
        ///     Converts this ReciprocalLength to another ReciprocalLength with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A ReciprocalLength with the specified unit.</returns>
        public ReciprocalLength ToUnit(ReciprocalLengthUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new ReciprocalLength(convertedValue, unit));
        }
Exemplo n.º 10
0
        /// <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 ReciprocalLengthUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <ReciprocalLengthUnit>(str, provider, out unit));
        }
Exemplo n.º 11
0
 public static bool TryParseUnit(string str, out ReciprocalLengthUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
Exemplo n.º 12
0
        /// <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(ReciprocalLengthUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
Exemplo n.º 13
0
 /// <summary>
 ///     Get unit abbreviation string.
 /// </summary>
 /// <param name="unit">Unit to get abbreviation for.</param>
 /// <returns>Unit abbreviation string.</returns>
 public static string GetAbbreviation(ReciprocalLengthUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }