Пример #1
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 AbsorbedDose(double value, AbsorbedDoseUnit unit)
        {
            if (unit == AbsorbedDoseUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
Пример #2
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(AbsorbedDoseUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Пример #3
0
        private double AsBaseNumericType(AbsorbedDoseUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case AbsorbedDoseUnit.Gray: return(baseUnitValue);

            case AbsorbedDoseUnit.Sievert: return(baseUnitValue);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Пример #4
0
        /// <summary>
        ///     Converts this AbsorbedDose to another AbsorbedDose with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A AbsorbedDose with the specified unit.</returns>
        public AbsorbedDose ToUnit(AbsorbedDoseUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new AbsorbedDose(convertedValue, unit));
        }
Пример #5
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 AbsorbedDoseUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <AbsorbedDoseUnit>(str, provider, out unit));
        }
Пример #6
0
 public static bool TryParseUnit(string str, out AbsorbedDoseUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
Пример #7
0
 public static AbsorbedDose From(double value, AbsorbedDoseUnit fromUnit)
 {
     return(new AbsorbedDose((double)value, fromUnit));
 }
Пример #8
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(AbsorbedDoseUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
Пример #9
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(AbsorbedDoseUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }