コード例 #1
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RatioChangeRateUnit unit)
        {
            return(unit switch
            {
                RatioChangeRateUnit.DecimalFractionPerSecond => (DecimalFractionsPerSecondInOneDecimalFractionPerSecond, DecimalFractionsPerSecondTolerance),
                RatioChangeRateUnit.PercentPerSecond => (PercentsPerSecondInOneDecimalFractionPerSecond, PercentsPerSecondTolerance),
                _ => throw new NotSupportedException()
            });
コード例 #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 RatioChangeRate(double value, RatioChangeRateUnit unit)
        {
            if (unit == RatioChangeRateUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
コード例 #3
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 RatioChangeRate(decimal value, RatioChangeRateUnit unit)
        {
            if (unit == RatioChangeRateUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
コード例 #4
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(RatioChangeRateUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
コード例 #5
0
        private double AsBaseNumericType(RatioChangeRateUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case RatioChangeRateUnit.DecimalFractionPerSecond: return(baseUnitValue);

            case RatioChangeRateUnit.PercentPerSecond: return(baseUnitValue * 1e2);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
コード例 #6
0
        /// <summary>
        ///     Converts this RatioChangeRate to another RatioChangeRate with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A RatioChangeRate with the specified unit.</returns>
        public RatioChangeRate ToUnit(RatioChangeRateUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new RatioChangeRate(convertedValue, unit));
        }
コード例 #7
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 RatioChangeRateUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <RatioChangeRateUnit>(str, provider, out unit));
        }
コード例 #8
0
 public static bool TryParseUnit(string str, out RatioChangeRateUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
コード例 #9
0
 public static RatioChangeRate From(double value, RatioChangeRateUnit fromUnit)
 {
     return(new RatioChangeRate((double)value, fromUnit));
 }
コード例 #10
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(RatioChangeRateUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
コード例 #11
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(RatioChangeRateUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
コード例 #12
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 RatioChangeRate(double value, RatioChangeRateUnit unit)
 {
     _value = value;
     _unit  = unit;
 }
コード例 #13
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 RatioChangeRate ToUnit(RatioChangeRateUnit unit)
        {
            var convertedValue = GetValueAs(unit);

            return(new RatioChangeRate(convertedValue, unit));
        }
コード例 #14
0
 /// <summary>
 ///     Convert to the unit representation <paramref name="unit" />.
 /// </summary>
 /// <returns>Value converted to the specified unit.</returns>
 public double As(RatioChangeRateUnit unit) => GetValueAs(unit);
コード例 #15
0
 public static RatioChangeRate From(decimal value, RatioChangeRateUnit fromUnit)
 {
     return(new RatioChangeRate((decimal)value, fromUnit));
 }