Example #1
0
        /// <summary>
        /// Creates an instance of if the base units class that represents the base units for a quantity.
        /// All quantities, both base and derived, can be represented by a combination of these seven base units.
        /// </summary>
        /// <param name="length">The length unit (L).</param>
        /// <param name="mass">The mass unit (M).</param>
        /// <param name="time">The time unit (T).</param>
        /// <param name="current">The electric current unit (I).</param>
        /// <param name="temperature">The temperature unit (Θ).</param>
        /// <param name="amount">The amount of substance unit (N).</param>
        /// <param name="luminousIntensity">The luminous intensity unit (J).</param>
        public BaseUnits(
            LengthUnit length                       = LengthUnit.Undefined,
            MassUnit mass                           = MassUnit.Undefined,
            DurationUnit time                       = DurationUnit.Undefined,
            ElectricCurrentUnit current             = ElectricCurrentUnit.Undefined,
            TemperatureUnit temperature             = TemperatureUnit.Undefined,
            AmountOfSubstanceUnit amount            = AmountOfSubstanceUnit.Undefined,
            LuminousIntensityUnit luminousIntensity = LuminousIntensityUnit.Undefined)
        {
            Length            = length;
            Mass              = mass;
            Time              = time;
            Current           = current;
            Temperature       = temperature;
            Amount            = amount;
            LuminousIntensity = luminousIntensity;

            IsFullyDefined = Length != LengthUnit.Undefined &&
                             Mass != MassUnit.Undefined &&
                             Time != DurationUnit.Undefined &&
                             Current != ElectricCurrentUnit.Undefined &&
                             Temperature != TemperatureUnit.Undefined &&
                             Amount != AmountOfSubstanceUnit.Undefined &&
                             LuminousIntensity != LuminousIntensityUnit.Undefined;
        }
Example #2
0
        public static string GetAbbreviation(AmountOfSubstanceUnit 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
        public string ToString(AmountOfSubstanceUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, provider, format));
        }
Example #4
0
        /// <summary>
        ///     Creates the quantity with the given numeric value and unit.
        /// </summary>
        /// <param name="numericValue">The numeric value  to contruct this quantity with.</param>
        /// <param name="unit">The unit representation to contruct 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 AmountOfSubstance(double numericValue, AmountOfSubstanceUnit unit)
        {
            if (unit == AmountOfSubstanceUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = unit;
        }
        /// <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 AmountOfSubstance(double value, AmountOfSubstanceUnit unit)
        {
            if (unit == AmountOfSubstanceUnit.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(AmountOfSubstanceUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Example #7
0
 /// <summary>
 /// Creates an instance of if the base units class that represents the base units for a quantity.
 /// All quantities, both base and derived, can be represented by a combination of these seven base units.
 /// </summary>
 /// <param name="length">The length unit (L).</param>
 /// <param name="mass">The mass unit (M).</param>
 /// <param name="time">The time unit (T).</param>
 /// <param name="current">The electric current unit (I).</param>
 /// <param name="temperature">The temperature unit (Θ).</param>
 /// <param name="amount">The amount of substance unit (N).</param>
 /// <param name="luminousIntensity">The luminous intensity unit (J).</param>
 public BaseUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current,
                  TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity)
 {
     Length            = length;
     Mass              = mass;
     Time              = time;
     Current           = current;
     Temperature       = temperature;
     Amount            = amount;
     LuminousIntensity = luminousIntensity;
 }
        // 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="AmountOfSubstanceUnit" /> to <see cref="AmountOfSubstance" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>AmountOfSubstance unit value.</returns>
        public static AmountOfSubstance?From(QuantityValue?value, AmountOfSubstanceUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case AmountOfSubstanceUnit.Centimole:
                return(FromCentimoles(value.Value));

            case AmountOfSubstanceUnit.CentipoundMole:
                return(FromCentipoundMoles(value.Value));

            case AmountOfSubstanceUnit.Decimole:
                return(FromDecimoles(value.Value));

            case AmountOfSubstanceUnit.DecipoundMole:
                return(FromDecipoundMoles(value.Value));

            case AmountOfSubstanceUnit.Kilomole:
                return(FromKilomoles(value.Value));

            case AmountOfSubstanceUnit.KilopoundMole:
                return(FromKilopoundMoles(value.Value));

            case AmountOfSubstanceUnit.Micromole:
                return(FromMicromoles(value.Value));

            case AmountOfSubstanceUnit.MicropoundMole:
                return(FromMicropoundMoles(value.Value));

            case AmountOfSubstanceUnit.Millimole:
                return(FromMillimoles(value.Value));

            case AmountOfSubstanceUnit.MillipoundMole:
                return(FromMillipoundMoles(value.Value));

            case AmountOfSubstanceUnit.Mole:
                return(FromMoles(value.Value));

            case AmountOfSubstanceUnit.Nanomole:
                return(FromNanomoles(value.Value));

            case AmountOfSubstanceUnit.NanopoundMole:
                return(FromNanopoundMoles(value.Value));

            case AmountOfSubstanceUnit.PoundMole:
                return(FromPoundMoles(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Example #9
0
                    0.625023438378939, AmountOfSubstanceUnit.Mole)]     // 10 grams Of Oxygen contain 0.625023438378939 Moles
        public void AmountOfSubstanceFromMassAndMolarMass(
            double massValue, MassUnit massUnit,
            double molarMassValue, MolarMassUnit molarMassUnit,
            double expectedAmountOfSubstanceValue, AmountOfSubstanceUnit expectedAmountOfSubstanceUnit, double tolerence = 1e-5)
        {
            var mass      = new Mass(massValue, massUnit);
            var molarMass = new MolarMass(molarMassValue, molarMassUnit);

            AmountOfSubstance amountOfSubstance = mass / molarMass;

            AssertEx.EqualTolerance(expectedAmountOfSubstanceValue, amountOfSubstance.As(expectedAmountOfSubstanceUnit), tolerence);
        }
Example #10
0
                    10 * KnownQuantities.MolarMassOfOxygen, MassUnit.Gram)]     // 10 Moles of Oxygen weight 10 times as much as 1 Mole of Oxygen (MolarMass)
        public void MassFromAmountOfSubstanceAndMolarMass(
            double amountOfSubstanceValue, AmountOfSubstanceUnit amountOfSubstanceUnit,
            double molarMassValue, MolarMassUnit molarMassUnit,
            double expectedMass, MassUnit expectedMassUnit, double tolerence = 1e-5)
        {
            AmountOfSubstance amountOfSubstance = new AmountOfSubstance(amountOfSubstanceValue, amountOfSubstanceUnit);
            MolarMass         molarMass         = new MolarMass(molarMassValue, molarMassUnit);

            Mass mass = amountOfSubstance * molarMass;

            AssertEx.EqualTolerance(expectedMass, mass.As(expectedMassUnit), tolerence);
        }
Example #11
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(AmountOfSubstanceUnit unit)
        {
            switch (unit)
            {
            case AmountOfSubstanceUnit.Centimole:
                return(Centimoles);

            case AmountOfSubstanceUnit.CentipoundMole:
                return(CentipoundMoles);

            case AmountOfSubstanceUnit.Decimole:
                return(Decimoles);

            case AmountOfSubstanceUnit.DecipoundMole:
                return(DecipoundMoles);

            case AmountOfSubstanceUnit.Kilomole:
                return(Kilomoles);

            case AmountOfSubstanceUnit.KilopoundMole:
                return(KilopoundMoles);

            case AmountOfSubstanceUnit.Micromole:
                return(Micromoles);

            case AmountOfSubstanceUnit.MicropoundMole:
                return(MicropoundMoles);

            case AmountOfSubstanceUnit.Millimole:
                return(Millimoles);

            case AmountOfSubstanceUnit.MillipoundMole:
                return(MillipoundMoles);

            case AmountOfSubstanceUnit.Mole:
                return(Moles);

            case AmountOfSubstanceUnit.Nanomole:
                return(Nanomoles);

            case AmountOfSubstanceUnit.NanopoundMole:
                return(NanopoundMoles);

            case AmountOfSubstanceUnit.PoundMole:
                return(PoundMoles);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
        private double AsBaseNumericType(AmountOfSubstanceUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

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

            case AmountOfSubstanceUnit.CentipoundMole: return((baseUnitValue / 453.59237) / 1e-2d);

            case AmountOfSubstanceUnit.DecapoundMole: return((baseUnitValue / 453.59237) / 1e1d);

            case AmountOfSubstanceUnit.Decimole: return((baseUnitValue) / 1e-1d);

            case AmountOfSubstanceUnit.DecipoundMole: return((baseUnitValue / 453.59237) / 1e-1d);

            case AmountOfSubstanceUnit.Kilomole: return((baseUnitValue) / 1e3d);

            case AmountOfSubstanceUnit.KilopoundMole: return((baseUnitValue / 453.59237) / 1e3d);

            case AmountOfSubstanceUnit.Megamole: return((baseUnitValue) / 1e6d);

            case AmountOfSubstanceUnit.Micromole: return((baseUnitValue) / 1e-6d);

            case AmountOfSubstanceUnit.MicropoundMole: return((baseUnitValue / 453.59237) / 1e-6d);

            case AmountOfSubstanceUnit.Millimole: return((baseUnitValue) / 1e-3d);

            case AmountOfSubstanceUnit.MillipoundMole: return((baseUnitValue / 453.59237) / 1e-3d);

            case AmountOfSubstanceUnit.Mole: return(baseUnitValue);

            case AmountOfSubstanceUnit.Nanomole: return((baseUnitValue) / 1e-9d);

            case AmountOfSubstanceUnit.NanopoundMole: return((baseUnitValue / 453.59237) / 1e-9d);

            case AmountOfSubstanceUnit.PoundMole: return(baseUnitValue / 453.59237);

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

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <AmountOfSubstance, AmountOfSubstanceUnit>(str, provider,
                                                                                   delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                AmountOfSubstanceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMoles(x.Moles + y.Moles)));
        }
Example #14
0
        public string ToString(AmountOfSubstanceUnit 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));
        }
Example #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="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 AmountOfSubstance 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 <AmountOfSubstance, AmountOfSubstanceUnit>(str, provider,
                                                                                   delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                AmountOfSubstanceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMoles(x.Moles + y.Moles)));
        }
Example #16
0
        public string ToString(AmountOfSubstanceUnit 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));
        }
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AmountOfSubstanceUnit unit)
        {
            return(unit switch
            {
                AmountOfSubstanceUnit.Centimole => (CentimolesInOneMole, CentimolesTolerance),
                AmountOfSubstanceUnit.CentipoundMole => (CentipoundMolesInOneMole, CentipoundMolesTolerance),
                AmountOfSubstanceUnit.Decimole => (DecimolesInOneMole, DecimolesTolerance),
                AmountOfSubstanceUnit.DecipoundMole => (DecipoundMolesInOneMole, DecipoundMolesTolerance),
                AmountOfSubstanceUnit.Kilomole => (KilomolesInOneMole, KilomolesTolerance),
                AmountOfSubstanceUnit.KilopoundMole => (KilopoundMolesInOneMole, KilopoundMolesTolerance),
                AmountOfSubstanceUnit.Megamole => (MegamolesInOneMole, MegamolesTolerance),
                AmountOfSubstanceUnit.Micromole => (MicromolesInOneMole, MicromolesTolerance),
                AmountOfSubstanceUnit.MicropoundMole => (MicropoundMolesInOneMole, MicropoundMolesTolerance),
                AmountOfSubstanceUnit.Millimole => (MillimolesInOneMole, MillimolesTolerance),
                AmountOfSubstanceUnit.MillipoundMole => (MillipoundMolesInOneMole, MillipoundMolesTolerance),
                AmountOfSubstanceUnit.Mole => (MolesInOneMole, MolesTolerance),
                AmountOfSubstanceUnit.Nanomole => (NanomolesInOneMole, NanomolesTolerance),
                AmountOfSubstanceUnit.NanopoundMole => (NanopoundMolesInOneMole, NanopoundMolesTolerance),
                AmountOfSubstanceUnit.PoundMole => (PoundMolesInOneMole, PoundMolesTolerance),
                _ => throw new NotSupportedException()
            });
Example #18
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 AmountOfSubstance 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 <AmountOfSubstance, AmountOfSubstanceUnit>(str, formatProvider,
                                                                                   delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                AmountOfSubstanceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMoles(x.Moles + y.Moles)));
        }
Example #19
0
        public string ToString(AmountOfSubstanceUnit 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));
        }
 public static bool TryParseUnit(string str, out AmountOfSubstanceUnit 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(AmountOfSubstanceUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
 public static AmountOfSubstance From(double value, AmountOfSubstanceUnit fromUnit)
 {
     return(new AmountOfSubstance((double)value, fromUnit));
 }
 /// <summary>
 ///     Get unit abbreviation string.
 /// </summary>
 /// <param name="unit">Unit to get abbreviation for.</param>
 /// <returns>Unit abbreviation string.</returns>
 public static string GetAbbreviation(AmountOfSubstanceUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
Example #24
0
        public static string GetAbbreviation(AmountOfSubstanceUnit unit, [CanBeNull] IFormatProvider provider)
        {
            provider = provider ?? UnitSystem.DefaultCulture;

            return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit));
        }
 /// <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 AmountOfSubstance(double value, AmountOfSubstanceUnit unit)
 {
     _value = value;
     _unit  = unit;
 }
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="AmountOfSubstanceUnit" /> to <see cref="AmountOfSubstance" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>AmountOfSubstance unit value.</returns>
 public static AmountOfSubstance?From(QuantityValue?value, AmountOfSubstanceUnit fromUnit)
 {
     return(value.HasValue ? new AmountOfSubstance((double)value.Value, fromUnit) : default(AmountOfSubstance?));
 }
        /// <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 AmountOfSubstanceUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <AmountOfSubstanceUnit>(str, provider, out unit));
        }
        /// <summary>
        ///     Converts this Duration to another Duration with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Duration with the specified unit.</returns>
        public AmountOfSubstance ToUnit(AmountOfSubstanceUnit unit)
        {
            var convertedValue = GetValueAs(unit);

            return(new AmountOfSubstance(convertedValue, unit));
        }
Example #29
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(AmountOfSubstanceUnit unit, [CanBeNull] IFormatProvider provider)
 {
     return(ToString(unit, provider, 2));
 }
        /// <summary>
        ///     Converts this AmountOfSubstance to another AmountOfSubstance with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A AmountOfSubstance with the specified unit.</returns>
        public AmountOfSubstance ToUnit(AmountOfSubstanceUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new AmountOfSubstance(convertedValue, unit));
        }
 protected static string CreateSuffix(SymbolFormat format, AmountOfSubstanceUnit unit)
 {
     return default(AmountOfSubstance).ToString(unit, format).Trim('0');
 }