Ejemplo n.º 1
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VitaminAUnit unit)
        {
            return(unit switch
            {
                VitaminAUnit.InternationalUnit => (InternationalUnitsInOneInternationalUnit, InternationalUnitsTolerance),
                _ => throw new NotSupportedException()
            });
Ejemplo n.º 2
0
        public string ToString(VitaminAUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, culture, format));
        }
        public static string GetAbbreviation(VitaminAUnit 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));
        }
Ejemplo n.º 4
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="VitaminAUnit" /> to <see cref="VitaminA" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>VitaminA unit value.</returns>
        public static VitaminA?From(QuantityValue?value, VitaminAUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }

            return(new VitaminA((double)value.Value, fromUnit));
        }
Ejemplo 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>
        /// <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 VitaminA(double value, VitaminAUnit unit)
        {
            if (unit == VitaminAUnit.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.º 6
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 VitaminA(decimal value, VitaminAUnit unit)
        {
            if (unit == VitaminAUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
Ejemplo n.º 7
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(VitaminAUnit unit)
        {
            switch (unit)
            {
            case VitaminAUnit.InternationalUnit:
                return(InternationalUnits);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(VitaminAUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="VitaminAUnit" /> to <see cref="VitaminA" />.
        /// </summary>
        /// <param name="val">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>VitaminA unit value.</returns>
        public static VitaminA From(double val, VitaminAUnit fromUnit)
        {
            switch (fromUnit)
            {
            case VitaminAUnit.InternationalUnit:
                return(FromInternationalUnits(val));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(VitaminAUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Ejemplo n.º 11
0
        public static VitaminA From(QuantityValue value, VitaminAUnit fromUnit)
#endif
        {
            switch (fromUnit)
            {
            case VitaminAUnit.InternationalUnit:
                return(FromInternationalUnits(value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        ///     Parse a string given a particular regular expression.
        /// </summary>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        private static List <VitaminA> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null)
        {
            var             regex     = new Regex(regexString);
            MatchCollection matches   = regex.Matches(str.Trim());
            var             converted = new List <VitaminA>();

            foreach (Match match in matches)
            {
                GroupCollection groups = match.Groups;

                var valueString = groups["value"].Value;
                var unitString  = groups["unit"].Value;
                if (groups["invalid"].Value != "")
                {
                    var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value);
                    newEx.Data["input"]          = str;
                    newEx.Data["matched value"]  = valueString;
                    newEx.Data["matched unit"]   = unitString;
                    newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                    throw newEx;
                }
                if (valueString == "" && unitString == "")
                {
                    continue;
                }

                try
                {
                    VitaminAUnit unit  = ParseUnit(unitString, formatProvider);
                    double       value = double.Parse(valueString, formatProvider);

                    converted.Add(From(value, unit));
                }
                catch (AmbiguousUnitParseException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    var newEx = new UnitsNetException("Error parsing string.", ex);
                    newEx.Data["input"]          = str;
                    newEx.Data["matched value"]  = valueString;
                    newEx.Data["matched unit"]   = unitString;
                    newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                    throw newEx;
                }
            }
            return(converted);
        }
Ejemplo n.º 13
0
        public string ToString(VitaminAUnit 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);
        }
Ejemplo n.º 14
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="VitaminAUnit" /> to <see cref="VitaminA" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>VitaminA unit value.</returns>
        public static VitaminA?From(double?value, VitaminAUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case VitaminAUnit.InternationalUnit:
                return(FromInternationalUnits(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Ejemplo n.º 15
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 VitaminA 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<VitaminA>(str, formatProvider,
                delegate(string value, string unit, IFormatProvider formatProvider2)
                {
                    double parsedValue = double.Parse(value, formatProvider2);
                    VitaminAUnit parsedUnit = ParseUnit(unit, formatProvider2);
                    return From(parsedValue, parsedUnit);
                }, (x, y) => FromInternationalUnits(x.InternationalUnits + y.InternationalUnits));
        }
Ejemplo n.º 16
0
        private double AsBaseNumericType(VitaminAUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case VitaminAUnit.InternationalUnit: return(baseUnitValue);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Ejemplo n.º 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="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 VitaminA Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <VitaminA, VitaminAUnit>(str, provider,
                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                VitaminAUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromInternationalUnits(x.InternationalUnits + y.InternationalUnits)));
        }
        /// <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 VitaminA 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 <VitaminA, VitaminAUnit>(str, provider,
                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                VitaminAUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromInternationalUnits(x.InternationalUnits + y.InternationalUnits)));
        }
Ejemplo n.º 19
0
        public string ToString(VitaminAUnit 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));
        }
        public string ToString(VitaminAUnit 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));
        }
Ejemplo n.º 21
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 VitaminA 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 <VitaminA, VitaminAUnit>(str, formatProvider,
                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                VitaminAUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromInternationalUnits(x.InternationalUnits + y.InternationalUnits)));
        }
Ejemplo n.º 22
0
        public string ToString(VitaminAUnit 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));
        }
Ejemplo n.º 23
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(VitaminAUnit unit)
 {
     return(ToString(unit, null, 2));
 }
Ejemplo n.º 24
0
        /// <summary>
        ///     Converts this VitaminA to another VitaminA with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A VitaminA with the specified unit.</returns>
        public VitaminA ToUnit(VitaminAUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new VitaminA(convertedValue, unit));
        }
Ejemplo n.º 25
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(VitaminAUnit unit, [CanBeNull] Culture culture)
 {
     return(ToString(unit, culture, 2));
 }
Ejemplo n.º 26
0
        public static string GetAbbreviation(
            VitaminAUnit unit,
#if WINDOWS_UWP
            [CanBeNull] string cultureName)
Ejemplo n.º 27
0
 public static string GetAbbreviation(VitaminAUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
Ejemplo n.º 28
0
        public static VitaminA From(QuantityValue value, VitaminAUnit fromUnit)
#endif
        {
            return(new VitaminA((double)value, fromUnit));
        }
Ejemplo n.º 29
0
 public static VitaminA From(double value, VitaminAUnit fromUnit)
Ejemplo n.º 30
0
 VitaminA(double numericValue, VitaminAUnit unit)
 {
     _value = numericValue;
     _unit  = unit;
 }