/// <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 Luminosity(double value, LuminosityUnit unit)
        {
            if (unit == LuminosityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
Beispiel #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 Luminosity(decimal value, LuminosityUnit unit)
        {
            if (unit == LuminosityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(LuminosityUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Beispiel #4
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(LuminosityUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
Beispiel #5
0
        private decimal AsBaseNumericType(LuminosityUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case LuminosityUnit.Decawatt: return((baseUnitValue) / 1e1m);

            case LuminosityUnit.Deciwatt: return((baseUnitValue) / 1e-1m);

            case LuminosityUnit.Femtowatt: return((baseUnitValue) / 1e-15m);

            case LuminosityUnit.Gigawatt: return((baseUnitValue) / 1e9m);

            case LuminosityUnit.Kilowatt: return((baseUnitValue) / 1e3m);

            case LuminosityUnit.Megawatt: return((baseUnitValue) / 1e6m);

            case LuminosityUnit.Microwatt: return((baseUnitValue) / 1e-6m);

            case LuminosityUnit.Milliwatt: return((baseUnitValue) / 1e-3m);

            case LuminosityUnit.Nanowatt: return((baseUnitValue) / 1e-9m);

            case LuminosityUnit.Petawatt: return((baseUnitValue) / 1e15m);

            case LuminosityUnit.Picowatt: return((baseUnitValue) / 1e-12m);

            case LuminosityUnit.SolarLuminosity: return(baseUnitValue / 3.846e26m);

            case LuminosityUnit.Terawatt: return((baseUnitValue) / 1e12m);

            case LuminosityUnit.Watt: return(baseUnitValue);

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

            var baseUnitValue = GetValueInBaseUnit();

            switch (unit)
            {
            case LuminosityUnit.Decawatt: return((baseUnitValue) / 1e1d);

            case LuminosityUnit.Deciwatt: return((baseUnitValue) / 1e-1d);

            case LuminosityUnit.Femtowatt: return((baseUnitValue) / 1e-15d);

            case LuminosityUnit.Gigawatt: return((baseUnitValue) / 1e9d);

            case LuminosityUnit.Kilowatt: return((baseUnitValue) / 1e3d);

            case LuminosityUnit.Megawatt: return((baseUnitValue) / 1e6d);

            case LuminosityUnit.Microwatt: return((baseUnitValue) / 1e-6d);

            case LuminosityUnit.Milliwatt: return((baseUnitValue) / 1e-3d);

            case LuminosityUnit.Nanowatt: return((baseUnitValue) / 1e-9d);

            case LuminosityUnit.Petawatt: return((baseUnitValue) / 1e15d);

            case LuminosityUnit.Picowatt: return((baseUnitValue) / 1e-12d);

            case LuminosityUnit.SolarLuminosity: return(baseUnitValue / 3.846e26);

            case LuminosityUnit.Terawatt: return((baseUnitValue) / 1e12d);

            case LuminosityUnit.Watt: return(baseUnitValue);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Beispiel #7
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LuminosityUnit unit)
        {
            return(unit switch
            {
                LuminosityUnit.Decawatt => (DecawattsInOneWatt, DecawattsTolerance),
                LuminosityUnit.Deciwatt => (DeciwattsInOneWatt, DeciwattsTolerance),
                LuminosityUnit.Femtowatt => (FemtowattsInOneWatt, FemtowattsTolerance),
                LuminosityUnit.Gigawatt => (GigawattsInOneWatt, GigawattsTolerance),
                LuminosityUnit.Kilowatt => (KilowattsInOneWatt, KilowattsTolerance),
                LuminosityUnit.Megawatt => (MegawattsInOneWatt, MegawattsTolerance),
                LuminosityUnit.Microwatt => (MicrowattsInOneWatt, MicrowattsTolerance),
                LuminosityUnit.Milliwatt => (MilliwattsInOneWatt, MilliwattsTolerance),
                LuminosityUnit.Nanowatt => (NanowattsInOneWatt, NanowattsTolerance),
                LuminosityUnit.Petawatt => (PetawattsInOneWatt, PetawattsTolerance),
                LuminosityUnit.Picowatt => (PicowattsInOneWatt, PicowattsTolerance),
                LuminosityUnit.SolarLuminosity => (SolarLuminositiesInOneWatt, SolarLuminositiesTolerance),
                LuminosityUnit.Terawatt => (TerawattsInOneWatt, TerawattsTolerance),
                LuminosityUnit.Watt => (WattsInOneWatt, WattsTolerance),
                _ => throw new NotSupportedException()
            });
Beispiel #8
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 Luminosity ToUnit(LuminosityUnit unit)
        {
            var convertedValue = GetValueAs(unit);

            return(new Luminosity(convertedValue, unit));
        }
Beispiel #9
0
 /// <summary>
 ///     Convert to the unit representation <paramref name="unit" />.
 /// </summary>
 /// <returns>Value converted to the specified unit.</returns>
 public double As(LuminosityUnit unit) => GetValueAs(unit);
Beispiel #10
0
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="LuminosityUnit" /> to <see cref="Luminosity" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>Luminosity unit value.</returns>
 public static Luminosity From(double value, LuminosityUnit fromUnit)
 {
     return(new Luminosity(value, fromUnit));
 }
        /// <summary>
        ///     Converts this Luminosity to another Luminosity with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Luminosity with the specified unit.</returns>
        public Luminosity ToUnit(LuminosityUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new Luminosity(convertedValue, unit));
        }
 public static bool TryParseUnit(string str, out LuminosityUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
        /// <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(LuminosityUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
 /// <summary>
 ///     Get unit abbreviation string.
 /// </summary>
 /// <param name="unit">Unit to get abbreviation for.</param>
 /// <returns>Unit abbreviation string.</returns>
 public static string GetAbbreviation(LuminosityUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
        /// <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 LuminosityUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <LuminosityUnit>(str, provider, out unit));
        }
Beispiel #16
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 Luminosity(double value, LuminosityUnit unit)
 {
     _value = value;
     _unit  = unit;
 }
Beispiel #17
0
 public static Luminosity From(decimal value, LuminosityUnit fromUnit)
 {
     return(new Luminosity((decimal)value, fromUnit));
 }