Ejemplo n.º 1
0
        private double AsBaseNumericType(CompressibilityUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case CompressibilityUnit.InverseAtmosphere: return(baseUnitValue / 101325);

            case CompressibilityUnit.InverseBar: return(baseUnitValue / 1e5);

            case CompressibilityUnit.InverseKilopascal: return(baseUnitValue / 1e3);

            case CompressibilityUnit.InverseMegapascal: return(baseUnitValue / 1e6);

            case CompressibilityUnit.InverseMillibar: return(baseUnitValue / 100);

            case CompressibilityUnit.InversePascal: return(baseUnitValue);

            case CompressibilityUnit.InversePoundForcePerSquareInch: return(baseUnitValue / 6.894757293168361e3);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Ejemplo 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 Compressibility(double value, CompressibilityUnit unit)
        {
            if (unit == CompressibilityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

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

            var converted = AsBaseNumericType(unit);

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

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(CompressibilityUnit unit)
        {
            return(unit switch
            {
                CompressibilityUnit.InverseAtmosphere => (InverseAtmospheresInOneInversePascal, InverseAtmospheresTolerance),
                CompressibilityUnit.InverseBar => (InverseBarsInOneInversePascal, InverseBarsTolerance),
                CompressibilityUnit.InverseKilopascal => (InverseKilopascalsInOneInversePascal, InverseKilopascalsTolerance),
                CompressibilityUnit.InverseMegapascal => (InverseMegapascalsInOneInversePascal, InverseMegapascalsTolerance),
                CompressibilityUnit.InverseMillibar => (InverseMillibarsInOneInversePascal, InverseMillibarsTolerance),
                CompressibilityUnit.InversePascal => (InversePascalsInOneInversePascal, InversePascalsTolerance),
                CompressibilityUnit.InversePoundForcePerSquareInch => (InversePoundsForcePerSquareInchInOneInversePascal, InversePoundsForcePerSquareInchTolerance),
                _ => throw new NotSupportedException()
            });
Ejemplo n.º 5
0
        /// <summary>
        ///     Converts this Compressibility to another Compressibility with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Compressibility with the specified unit.</returns>
        public Compressibility ToUnit(CompressibilityUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new Compressibility(convertedValue, unit));
        }
Ejemplo n.º 6
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 CompressibilityUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <CompressibilityUnit>(str, provider, out unit));
        }
Ejemplo n.º 7
0
 public static bool TryParseUnit(string str, out CompressibilityUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
Ejemplo n.º 8
0
 public static Compressibility From(double value, CompressibilityUnit fromUnit)
 {
     return(new Compressibility((double)value, fromUnit));
 }
Ejemplo n.º 9
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(CompressibilityUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
Ejemplo n.º 10
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(CompressibilityUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
Ejemplo n.º 11
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 Compressibility(double value, CompressibilityUnit unit)
 {
     _value = value;
     _unit  = unit;
 }
Ejemplo n.º 12
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 Compressibility ToUnit(CompressibilityUnit unit)
        {
            var convertedValue = GetValueAs(unit);

            return(new Compressibility(convertedValue, unit));
        }
Ejemplo n.º 13
0
 /// <summary>
 ///     Convert to the unit representation <paramref name="unit" />.
 /// </summary>
 /// <returns>Value converted to the specified unit.</returns>
 public double As(CompressibilityUnit unit) => GetValueAs(unit);