Example #1
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value in new unit if successful, exception otherwise.</returns>
        /// <exception cref="NotImplementedException">If conversion was not successful.</exception>
        public double As(TemperatureDeltaUnit unit)
        {
            switch (unit)
            {
            case TemperatureDeltaUnit.DegreeCelsiusDelta:
                return(DegreesCelsiusDelta);

            case TemperatureDeltaUnit.DegreeDelisleDelta:
                return(DegreesDelisleDelta);

            case TemperatureDeltaUnit.DegreeFahrenheitDelta:
                return(DegreesFahrenheitDelta);

            case TemperatureDeltaUnit.DegreeNewtonDelta:
                return(DegreesNewtonDelta);

            case TemperatureDeltaUnit.DegreeRankineDelta:
                return(DegreesRankineDelta);

            case TemperatureDeltaUnit.DegreeReaumurDelta:
                return(DegreesReaumurDelta);

            case TemperatureDeltaUnit.DegreeRoemerDelta:
                return(DegreesRoemerDelta);

            case TemperatureDeltaUnit.KelvinDelta:
                return(KelvinsDelta);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
        public static string GetAbbreviation(TemperatureDeltaUnit unit, [CanBeNull] string cultureName)
        {
            // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
            IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);

            return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit));
        }
Example #3
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="TemperatureDeltaUnit" /> to <see cref="TemperatureDelta" />.
        /// </summary>
        /// <param name="val">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>TemperatureDelta unit value.</returns>
        public static TemperatureDelta From(double val, TemperatureDeltaUnit fromUnit)
        {
            switch (fromUnit)
            {
            case TemperatureDeltaUnit.DegreeCelsiusDelta:
                return(FromDegreesCelsiusDelta(val));

            case TemperatureDeltaUnit.DegreeDelisleDelta:
                return(FromDegreesDelisleDelta(val));

            case TemperatureDeltaUnit.DegreeFahrenheitDelta:
                return(FromDegreesFahrenheitDelta(val));

            case TemperatureDeltaUnit.DegreeNewtonDelta:
                return(FromDegreesNewtonDelta(val));

            case TemperatureDeltaUnit.DegreeRankineDelta:
                return(FromDegreesRankineDelta(val));

            case TemperatureDeltaUnit.DegreeReaumurDelta:
                return(FromDegreesReaumurDelta(val));

            case TemperatureDeltaUnit.DegreeRoemerDelta:
                return(FromDegreesRoemerDelta(val));

            case TemperatureDeltaUnit.KelvinDelta:
                return(FromKelvinsDelta(val));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Example #4
0
        public string ToString(TemperatureDeltaUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, provider, format));
        }
Example #5
0
        private decimal AsBaseNumericType(TemperatureDeltaUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case TemperatureDeltaUnit.DegreeCelsius: return(baseUnitValue);

            case TemperatureDeltaUnit.DegreeDelisle: return(baseUnitValue * -3m / 2m);

            case TemperatureDeltaUnit.DegreeFahrenheit: return(baseUnitValue * 9m / 5m);

            case TemperatureDeltaUnit.DegreeNewton: return(baseUnitValue * 33m / 100m);

            case TemperatureDeltaUnit.DegreeRankine: return(baseUnitValue * 9m / 5m);

            case TemperatureDeltaUnit.DegreeReaumur: return(baseUnitValue * 4m / 5m);

            case TemperatureDeltaUnit.DegreeRoemer: return(baseUnitValue * 21m / 40m);

            case TemperatureDeltaUnit.Kelvin: return(baseUnitValue);

            case TemperatureDeltaUnit.MillidegreeCelsius: return((baseUnitValue) / 1e-3m);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Example #6
0
        private double GetValueAs(TemperatureDeltaUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = GetValueInBaseUnit();

            switch (unit)
            {
            case TemperatureDeltaUnit.DegreeCelsius: return(baseUnitValue);

            case TemperatureDeltaUnit.DegreeDelisle: return(baseUnitValue * -3 / 2);

            case TemperatureDeltaUnit.DegreeFahrenheit: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeNewton: return(baseUnitValue * 33 / 100);

            case TemperatureDeltaUnit.DegreeRankine: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeReaumur: return(baseUnitValue * 4 / 5);

            case TemperatureDeltaUnit.DegreeRoemer: return(baseUnitValue * 21 / 40);

            case TemperatureDeltaUnit.Kelvin: return(baseUnitValue);

            case TemperatureDeltaUnit.MillidegreeCelsius: return((baseUnitValue) / 1e-3d);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Example #7
0
        private double AsBaseNumericType(TemperatureDeltaUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case TemperatureDeltaUnit.DegreeCelsius: return(baseUnitValue);

            case TemperatureDeltaUnit.DegreeCelsiusDelta: return(baseUnitValue);

            case TemperatureDeltaUnit.DegreeFahrenheit: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeFahrenheitDelta: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeNewton: return(baseUnitValue * 33 / 100);

            case TemperatureDeltaUnit.DegreeNewtonDelta: return(baseUnitValue * 33 / 100);

            case TemperatureDeltaUnit.DegreeRankine: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeRankineDelta: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.Kelvin: return(baseUnitValue);

            case TemperatureDeltaUnit.KelvinDelta: return(baseUnitValue);

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

            _value = value;
            _unit  = unit;
        }
Example #9
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 TemperatureDelta(double value, TemperatureDeltaUnit unit)
        {
            if (unit == TemperatureDeltaUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
Example #10
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(TemperatureDeltaUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
Example #11
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TemperatureDeltaUnit unit)
        {
            return(unit switch
            {
                TemperatureDeltaUnit.DegreeCelsius => (DegreesCelsiusInOneKelvin, DegreesCelsiusTolerance),
                TemperatureDeltaUnit.DegreeDelisle => (DegreesDelisleInOneKelvin, DegreesDelisleTolerance),
                TemperatureDeltaUnit.DegreeFahrenheit => (DegreesFahrenheitInOneKelvin, DegreesFahrenheitTolerance),
                TemperatureDeltaUnit.DegreeNewton => (DegreesNewtonInOneKelvin, DegreesNewtonTolerance),
                TemperatureDeltaUnit.DegreeRankine => (DegreesRankineInOneKelvin, DegreesRankineTolerance),
                TemperatureDeltaUnit.DegreeReaumur => (DegreesReaumurInOneKelvin, DegreesReaumurTolerance),
                TemperatureDeltaUnit.DegreeRoemer => (DegreesRoemerInOneKelvin, DegreesRoemerTolerance),
                TemperatureDeltaUnit.Kelvin => (KelvinsInOneKelvin, KelvinsTolerance),
                TemperatureDeltaUnit.MillidegreeCelsius => (MillidegreesCelsiusInOneKelvin, MillidegreesCelsiusTolerance),
                _ => throw new NotSupportedException()
            });
Example #12
0
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
        /// <example>
        ///     Length.Parse("5.5 m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="ArgumentException">
        ///     Expected string to have one or two pairs of quantity and unit in the format
        ///     "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
        /// </exception>
        /// <exception cref="AmbiguousUnitParseException">
        ///     More than one unit is represented by the specified unit abbreviation.
        ///     Example: Volume.Parse("1 cup") will throw, because it can refer to any of
        ///     <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
        /// </exception>
        /// <exception cref="UnitsNetException">
        ///     If anything else goes wrong, typically due to a bug or unhandled case.
        ///     We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
        ///     Units.NET exceptions from other exceptions.
        /// </exception>
        public static TemperatureDelta Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <TemperatureDelta, TemperatureDeltaUnit>(str, provider,
                                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TemperatureDeltaUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKelvins(x.Kelvins + y.Kelvins)));
        }
Example #13
0
        private double AsBaseNumericType(TemperatureDeltaUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case TemperatureDeltaUnit.CentidegreeCelsius: return((baseUnitValue) / 1e-2d);

            case TemperatureDeltaUnit.DecidegreeCelsius: return((baseUnitValue) / 1e-1d);

            case TemperatureDeltaUnit.DegreeCelsius: return(baseUnitValue);

            case TemperatureDeltaUnit.DegreeDelisle: return(baseUnitValue * -3 / 2);

            case TemperatureDeltaUnit.DegreeFahrenheit: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeNewton: return(baseUnitValue * 33 / 100);

            case TemperatureDeltaUnit.DegreeRankine: return(baseUnitValue * 9 / 5);

            case TemperatureDeltaUnit.DegreeReaumur: return(baseUnitValue * 4 / 5);

            case TemperatureDeltaUnit.DegreeRoemer: return(baseUnitValue * 21 / 40);

            case TemperatureDeltaUnit.HectodegreeCelsius: return((baseUnitValue) / 1e2d);

            case TemperatureDeltaUnit.Kelvin: return(baseUnitValue);

            case TemperatureDeltaUnit.KilodegreeCelsius: return((baseUnitValue) / 1e3d);

            case TemperatureDeltaUnit.MicrodegreeCelsius: return((baseUnitValue) / 1e-6d);

            case TemperatureDeltaUnit.MillidegreeCelsius: return((baseUnitValue) / 1e-3d);

            case TemperatureDeltaUnit.NanodegreeCelsius: return((baseUnitValue) / 1e-9d);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Example #14
0
        public string ToString(TemperatureDeltaUnit unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            double value = As(unit);

            object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args);
            return(string.Format(provider, format, formatArgs));
        }
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
        /// <example>
        ///     Length.Parse("5.5 m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="ArgumentException">
        ///     Expected string to have one or two pairs of quantity and unit in the format
        ///     "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
        /// </exception>
        /// <exception cref="AmbiguousUnitParseException">
        ///     More than one unit is represented by the specified unit abbreviation.
        ///     Example: Volume.Parse("1 cup") will throw, because it can refer to any of
        ///     <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
        /// </exception>
        /// <exception cref="UnitsNetException">
        ///     If anything else goes wrong, typically due to a bug or unhandled case.
        ///     We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
        ///     Units.NET exceptions from other exceptions.
        /// </exception>
        public static TemperatureDelta Parse(string str, [CanBeNull] string cultureName)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
            IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);

            return(QuantityParser.Parse <TemperatureDelta, TemperatureDeltaUnit>(str, provider,
                                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TemperatureDeltaUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKelvins(x.Kelvins + y.Kelvins)));
        }
        public string ToString(TemperatureDeltaUnit unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
            IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);

            double value = As(unit);

            object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args);
            return(string.Format(provider, format, formatArgs));
        }
Example #17
0
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
        /// <example>
        ///     Length.Parse("5.5 m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="ArgumentException">
        ///     Expected string to have one or two pairs of quantity and unit in the format
        ///     "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
        /// </exception>
        /// <exception cref="AmbiguousUnitParseException">
        ///     More than one unit is represented by the specified unit abbreviation.
        ///     Example: Volume.Parse("1 cup") will throw, because it can refer to any of
        ///     <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
        /// </exception>
        /// <exception cref="UnitsNetException">
        ///     If anything else goes wrong, typically due to a bug or unhandled case.
        ///     We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
        ///     Units.NET exceptions from other exceptions.
        /// </exception>
        public static TemperatureDelta Parse(string str, [CanBeNull] Culture culture)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }

#if WINDOWS_UWP
            IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture);
#else
            IFormatProvider formatProvider = culture;
#endif
            return(UnitParser.ParseUnit <TemperatureDelta>(str, formatProvider,
                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TemperatureDeltaUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKelvinsDelta(x.KelvinsDelta + y.KelvinsDelta)));
        }
Example #18
0
        public string ToString(TemperatureDeltaUnit unit, [CanBeNull] Culture culture, [NotNull] string format,
                               [NotNull] params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

#if WINDOWS_UWP
            IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture);
#else
            IFormatProvider formatProvider = culture;
#endif
            double   value      = As(unit);
            object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args);
            return(string.Format(formatProvider, format, formatArgs));
        }
Example #19
0
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
        /// <example>
        ///     Length.Parse("5.5 m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="ArgumentException">
        ///     Expected string to have one or two pairs of quantity and unit in the format
        ///     "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
        /// </exception>
        /// <exception cref="AmbiguousUnitParseException">
        ///     More than one unit is represented by the specified unit abbreviation.
        ///     Example: Volume.Parse("1 cup") will throw, because it can refer to any of
        ///     <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
        /// </exception>
        /// <exception cref="UnitsNetException">
        ///     If anything else goes wrong, typically due to a bug or unhandled case.
        ///     We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
        ///     Units.NET exceptions from other exceptions.
        /// </exception>
        public static TemperatureDelta Parse(string str, [CanBeNull] Culture culture)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }

            // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx
#if WINDOWS_UWP
            IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture);
#else
            IFormatProvider formatProvider = culture;
#endif
            return(QuantityParser.Parse <TemperatureDelta, TemperatureDeltaUnit>(str, formatProvider,
                                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TemperatureDeltaUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKelvinsDelta(x.KelvinsDelta + y.KelvinsDelta)));
        }
Example #20
0
        public string ToString(TemperatureDeltaUnit unit, [CanBeNull] Culture culture, [NotNull] string format,
                               [NotNull] params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx
#if WINDOWS_UWP
            IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture);
#else
            IFormatProvider formatProvider = culture;
#endif
            double   value      = As(unit);
            object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args);
            return(string.Format(formatProvider, format, formatArgs));
        }
Example #21
0
        // Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx
#if !WINDOWS_UWP
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="TemperatureDeltaUnit" /> to <see cref="TemperatureDelta" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>TemperatureDelta unit value.</returns>
        public static TemperatureDelta?From(QuantityValue?value, TemperatureDeltaUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case TemperatureDeltaUnit.DegreeCelsiusDelta:
                return(FromDegreesCelsiusDelta(value.Value));

            case TemperatureDeltaUnit.DegreeDelisleDelta:
                return(FromDegreesDelisleDelta(value.Value));

            case TemperatureDeltaUnit.DegreeFahrenheitDelta:
                return(FromDegreesFahrenheitDelta(value.Value));

            case TemperatureDeltaUnit.DegreeNewtonDelta:
                return(FromDegreesNewtonDelta(value.Value));

            case TemperatureDeltaUnit.DegreeRankineDelta:
                return(FromDegreesRankineDelta(value.Value));

            case TemperatureDeltaUnit.DegreeReaumurDelta:
                return(FromDegreesReaumurDelta(value.Value));

            case TemperatureDeltaUnit.DegreeRoemerDelta:
                return(FromDegreesRoemerDelta(value.Value));

            case TemperatureDeltaUnit.KelvinDelta:
                return(FromKelvinsDelta(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Example #22
0
        /// <summary>
        ///     Converts this TemperatureDelta to another TemperatureDelta with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A TemperatureDelta with the specified unit.</returns>
        public TemperatureDelta ToUnit(TemperatureDeltaUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new TemperatureDelta(convertedValue, unit));
        }
Example #23
0
 public static TemperatureDelta From(double value, TemperatureDeltaUnit fromUnit)
 {
     return(new TemperatureDelta((double)value, fromUnit));
 }
Example #24
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 TemperatureDeltaUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <TemperatureDeltaUnit>(str, provider, out unit));
        }
Example #25
0
 public static bool TryParseUnit(string str, out TemperatureDeltaUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
Example #26
0
 public static TemperatureDelta From(decimal value, TemperatureDeltaUnit fromUnit)
 {
     return(new TemperatureDelta((decimal)value, fromUnit));
 }
Example #27
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(TemperatureDeltaUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
Example #28
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(TemperatureDeltaUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
Example #29
0
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="TemperatureDeltaUnit" /> to <see cref="TemperatureDelta" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>TemperatureDelta unit value.</returns>
 public static TemperatureDelta?From(QuantityValue?value, TemperatureDeltaUnit fromUnit)
 {
     return(value.HasValue ? new TemperatureDelta((double)value.Value, fromUnit) : default(TemperatureDelta?));
 }
Example #30
0
 /// <summary>
 ///     Get string representation of value and unit. Using two significant digits after radix.
 /// </summary>
 /// <param name="unit">Unit representation to use.</param>
 /// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
 /// <returns>String representation.</returns>
 public string ToString(TemperatureDeltaUnit unit, [CanBeNull] IFormatProvider provider)
 {
     return(ToString(unit, provider, 2));
 }