示例#1
0
        public string ToString(PerTemperatureUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, culture, format));
        }
示例#2
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(PerTemperatureUnit unit)
        {
            switch (unit)
            {
            case PerTemperatureUnit.PerDegreeCelsius:
                return(PerDegreesCelsius);

            case PerTemperatureUnit.PerDegreeDelisle:
                return(PerDegreesDelisle);

            case PerTemperatureUnit.PerDegreeFahrenheit:
                return(PerDegreesFahrenheit);

            case PerTemperatureUnit.PerDegreeNewton:
                return(PerDegreesNewton);

            case PerTemperatureUnit.PerDegreeRankine:
                return(PerDegreesRankine);

            case PerTemperatureUnit.PerDegreeReaumur:
                return(PerDegreesReaumur);

            case PerTemperatureUnit.PerDegreeRoemer:
                return(PerDegreesRoemer);

            case PerTemperatureUnit.PerKelvin:
                return(PerKelvins);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
示例#3
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="PerTemperatureUnit" /> to <see cref="PerTemperature" />.
        /// </summary>
        /// <param name="val">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>PerTemperature unit value.</returns>
        public static PerTemperature From(double val, PerTemperatureUnit fromUnit)
        {
            switch (fromUnit)
            {
            case PerTemperatureUnit.PerDegreeCelsius:
                return(FromPerDegreesCelsius(val));

            case PerTemperatureUnit.PerDegreeDelisle:
                return(FromPerDegreesDelisle(val));

            case PerTemperatureUnit.PerDegreeFahrenheit:
                return(FromPerDegreesFahrenheit(val));

            case PerTemperatureUnit.PerDegreeNewton:
                return(FromPerDegreesNewton(val));

            case PerTemperatureUnit.PerDegreeRankine:
                return(FromPerDegreesRankine(val));

            case PerTemperatureUnit.PerDegreeReaumur:
                return(FromPerDegreesReaumur(val));

            case PerTemperatureUnit.PerDegreeRoemer:
                return(FromPerDegreesRoemer(val));

            case PerTemperatureUnit.PerKelvin:
                return(FromPerKelvins(val));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
示例#4
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 PerTemperature 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 <PerTemperature>(str, formatProvider,
                                                         delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                PerTemperatureUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromPerKelvins(x.PerKelvins + y.PerKelvins)));
        }
示例#5
0
        public string ToString(PerTemperatureUnit 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));
        }
示例#6
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="culture">Culture to use for localization and number formatting.</param>
 /// <returns>String representation.</returns>
 public string ToString(PerTemperatureUnit unit, [CanBeNull] Culture culture)
 {
     return(ToString(unit, culture, 2));
 }
示例#7
0
 /// <summary>
 ///     Get string representation of value and unit. Using current UI culture and two significant digits after radix.
 /// </summary>
 /// <param name="unit">Unit representation to use.</param>
 /// <returns>String representation.</returns>
 public string ToString(PerTemperatureUnit unit)
 {
     return(ToString(unit, null, 2));
 }
示例#8
0
 public static string GetAbbreviation(PerTemperatureUnit unit, [CanBeNull] Culture culture)
 {
     return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit));
 }
示例#9
0
 public static string GetAbbreviation(PerTemperatureUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }