Exemplo n.º 1
0
        private double AsBaseNumericType(MolarityUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case MolarityUnit.CentimolesPerLiter: return((baseUnitValue * 1e-3) / 1e-2d);

            case MolarityUnit.DecimolesPerLiter: return((baseUnitValue * 1e-3) / 1e-1d);

            case MolarityUnit.MicromolesPerLiter: return((baseUnitValue * 1e-3) / 1e-6d);

            case MolarityUnit.MillimolesPerLiter: return((baseUnitValue * 1e-3) / 1e-3d);

            case MolarityUnit.MolesPerCubicMeter: return(baseUnitValue);

            case MolarityUnit.MolesPerLiter: return(baseUnitValue * 1e-3);

            case MolarityUnit.NanomolesPerLiter: return((baseUnitValue * 1e-3) / 1e-9d);

            case MolarityUnit.PicomolesPerLiter: return((baseUnitValue * 1e-3) / 1e-12d);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
        public static string GetAbbreviation(MolarityUnit 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));
        }
Exemplo n.º 3
0
        public static Molarity From(QuantityValue value, MolarityUnit fromUnit)
#endif
        {
            switch (fromUnit)
            {
            case MolarityUnit.CentimolesPerLiter:
                return(FromCentimolesPerLiter(value));

            case MolarityUnit.DecimolesPerLiter:
                return(FromDecimolesPerLiter(value));

            case MolarityUnit.MicromolesPerLiter:
                return(FromMicromolesPerLiter(value));

            case MolarityUnit.MillimolesPerLiter:
                return(FromMillimolesPerLiter(value));

            case MolarityUnit.MolesPerCubicMeter:
                return(FromMolesPerCubicMeter(value));

            case MolarityUnit.MolesPerLiter:
                return(FromMolesPerLiter(value));

            case MolarityUnit.NanomolesPerLiter:
                return(FromNanomolesPerLiter(value));

            case MolarityUnit.PicomolesPerLiter:
                return(FromPicomolesPerLiter(value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Exemplo n.º 4
0
        public string ToString(MolarityUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, culture, format));
        }
Exemplo n.º 5
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(MolarityUnit unit)
        {
            switch (unit)
            {
            case MolarityUnit.CentimolesPerLiter:
                return(CentimolesPerLiter);

            case MolarityUnit.DecimolesPerLiter:
                return(DecimolesPerLiter);

            case MolarityUnit.MicromolesPerLiter:
                return(MicromolesPerLiter);

            case MolarityUnit.MillimolesPerLiter:
                return(MillimolesPerLiter);

            case MolarityUnit.MolesPerCubicMeter:
                return(MolesPerCubicMeter);

            case MolarityUnit.MolesPerLiter:
                return(MolesPerLiter);

            case MolarityUnit.NanomolesPerLiter:
                return(NanomolesPerLiter);

            case MolarityUnit.PicomolesPerLiter:
                return(PicomolesPerLiter);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
Exemplo n.º 6
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="MolarityUnit" /> to <see cref="Molarity" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>Molarity unit value.</returns>
        public static Molarity?From(QuantityValue?value, MolarityUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }

            return(new Molarity((double)value.Value, fromUnit));
        }
Exemplo n.º 7
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 Molarity(double value, MolarityUnit unit)
        {
            if (unit == MolarityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
Exemplo n.º 8
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 Molarity(double numericValue, MolarityUnit unit)
        {
            if (unit == MolarityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = unit;
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(MolarityUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(MolarityUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
Exemplo n.º 11
0
                    1026.98355, MolarityUnit.MolesPerCubicMeter)]    // test from JonathanDavies626
        public void MolarityFromMassConcentrationAndMolarMass(
            double massConcValue, MassConcentrationUnit massConcUnit,
            double molarMassValue, MolarMassUnit molarMassUnit,
            double expectedMolarityValue, MolarityUnit expectedMolarityUnit, double tolerance = 1e-5)
        {
            var massConcentration = new MassConcentration(massConcValue, massConcUnit);
            var molarMass         = new MolarMass(molarMassValue, molarMassUnit);

            Molarity molarity = massConcentration.ToMolarity(molarMass);     // molarity / molarMass

            AssertEx.EqualTolerance(expectedMolarityValue, molarity.As(expectedMolarityUnit), tolerance);
        }
Exemplo n.º 12
0
                    0.01142805, MolarityUnit.MolePerLiter)]    // 10 % dilution of HCL
        public void MolarityFromDilutedSolution(
            double startingMolarityValue, MolarityUnit startingMolarityUnit,
            double newConcentration, VolumeConcentrationUnit newConcentrationUnit,
            double expectedMolarityValue, MolarityUnit expectedMolarityUnit, double tolerence = 1e-5)
        {
            var startingMolarity = new Molarity(startingMolarityValue, startingMolarityUnit);
            var newVolumeConc    = new VolumeConcentration(newConcentration, newConcentrationUnit);

            Molarity dilutedMolarity = startingMolarity * newVolumeConc;

            AssertEx.EqualTolerance(expectedMolarityValue, dilutedMolarity.As(expectedMolarityUnit), tolerence);
        }
Exemplo n.º 13
0
                    4.16667, MassConcentrationUnit.KilogramPerCubicMeter)]    // HCL solution
        public void ExpectMolarityConvertedToMassConcentrationCorrectly(
            double molarityValue, MolarityUnit molarityUnit,
            double componentMolarMassValue, MolarMassUnit compontMolarMassUnit,
            double expectedMassConcValue, MassConcentrationUnit expectedMassConcUnit, double tolerence = 1e-5)
        {
            var molarity           = new Molarity(molarityValue, molarityUnit);
            var componentMolarMass = new MolarMass(componentMolarMassValue, compontMolarMassUnit);

            MassConcentration concentration = molarity.ToMassConcentration(componentMolarMass);  // molarity * molarMass

            AssertEx.EqualTolerance(expectedMassConcValue, concentration.As(expectedMassConcUnit), tolerence);
        }
Exemplo n.º 14
0
                    0.5, MolarityUnit.MolesPerLiter)]   // 29.19419518377693 = VolumeConcentration_0_5M_Ethanol
        public void MolarityFromVolumeConcentrationAndComponentDensityAndMolarMass(
            double volumeConcValue, VolumeConcentrationUnit volumeConcUnit,
            double componentDensityValue, DensityUnit componetDensityUnit,
            double componentMolarMassValue, MolarMassUnit componentMolarMassUnit,
            double expectedMolarityValue, MolarityUnit expectedMolarityUnit, double tolerence = 1e-5)
        {
            var volumeConcentration = new VolumeConcentration(volumeConcValue, volumeConcUnit);
            var componentDensity    = new Density(componentDensityValue, componetDensityUnit);
            var componentMolarMass  = new MolarMass(componentMolarMassValue, componentMolarMassUnit);

            Molarity molarity = volumeConcentration.ToMolarity(componentDensity, componentMolarMass); // volumeConcentration * density / molarMass

            AssertEx.EqualTolerance(expectedMolarityValue, molarity.As(expectedMolarityUnit), tolerence);
        }
Exemplo n.º 15
0
                    29.19419518377693, VolumeConcentrationUnit.MillilitersPerLiter)]    // 0.5M ethanol
        public void VolumeConcentrationFromComponentDensityAndMolarity(
            double molarityValue, MolarityUnit molarityUnit,
            double componentDensityValue, DensityUnit componentDensityUnit,
            double componentMolarMassValue, MolarMassUnit compontMolarMassUnit,
            double expectedVolumeConcValue, VolumeConcentrationUnit expectedVolumeConcUnit, double tolerence = 1e-5)
        {
            var molarity           = new Molarity(molarityValue, molarityUnit);
            var componentDensity   = new Density(componentDensityValue, componentDensityUnit);
            var componentMolarMass = new MolarMass(componentMolarMassValue, compontMolarMassUnit);

            VolumeConcentration volumeConcentration = molarity.ToVolumeConcentration(componentDensity, componentMolarMass);

            AssertEx.EqualTolerance(expectedVolumeConcValue, volumeConcentration.As(expectedVolumeConcUnit), tolerence);
        }
Exemplo n.º 16
0
                    0.1142805, MolarityUnit.MolesPerLiter)]     // molarity(HCl) = 5g / (1.2L * 36.46) = 0.114 mol/l = 0.114 M
        public void MolarityFromComponentMassAndSolutionVolume(
            double componentMassValue, MassUnit componentMassUnit,
            double componentMolarMassValue, MolarMassUnit componentMolarMassUnit,
            double solutionVolumeValue, VolumeUnit solutionVolumeUnit,
            double expectedMolarityValue, MolarityUnit expectedMolarityUnit, double tolerence = 1e-5)
        {
            var componentMass      = new Mass(componentMassValue, componentMassUnit);
            var componentMolarMass = new MolarMass(componentMolarMassValue, componentMolarMassUnit);
            var volumeSolution     = new Volume(solutionVolumeValue, solutionVolumeUnit);
            AmountOfSubstance amountOfSubstance = componentMass / componentMolarMass;

            Molarity molarity = amountOfSubstance / volumeSolution;

            AssertEx.EqualTolerance(expectedMolarityValue, molarity.As(expectedMolarityUnit), tolerence);
        }
Exemplo n.º 17
0
                    1.2, VolumeUnit.Liter)]     // 1.2 L of solution required for obtaining 0.1142805 Moles/L from 5g HCl
        public void VolumeSolutionFromComponentMassAndDesiredConcentration(
            double componentMassValue, MassUnit componentMassUnit,
            double componentMolarMassValue, MolarMassUnit componentMolarMassUnit,
            double desiredMolarityValue, MolarityUnit desiredMolarityUnit,
            double expectedSolutionVolumeValue, VolumeUnit expectedSolutionVolumeUnit, double tolerence = 1e-5)
        {
            var componentMass      = new Mass(componentMassValue, componentMassUnit);
            var componentMolarMass = new MolarMass(componentMolarMassValue, componentMolarMassUnit);
            var desiredMolarity    = new Molarity(desiredMolarityValue, desiredMolarityUnit);
            AmountOfSubstance amountOfSubstance = componentMass / componentMolarMass;

            Volume volumeSolution = amountOfSubstance / desiredMolarity;

            AssertEx.EqualTolerance(expectedSolutionVolumeValue, volumeSolution.As(expectedSolutionVolumeUnit), tolerence);
        }
        /// <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 Molarity Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <Molarity, MolarityUnit>(str, provider,
                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                MolarityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMolesPerCubicMeter(x.MolesPerCubicMeter + y.MolesPerCubicMeter)));
        }
        public string ToString(MolarityUnit 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));
        }
        /// <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 Molarity 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 <Molarity, MolarityUnit>(str, provider,
                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                MolarityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMolesPerCubicMeter(x.MolesPerCubicMeter + y.MolesPerCubicMeter)));
        }
        public string ToString(MolarityUnit 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));
        }
Exemplo n.º 22
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 Molarity 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 <Molarity>(str, formatProvider,
                                                   delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                MolarityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMolesPerCubicMeter(x.MolesPerCubicMeter + y.MolesPerCubicMeter)));
        }
Exemplo n.º 23
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 Molarity 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 <Molarity, MolarityUnit>(str, formatProvider,
                                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                MolarityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromMolesPerCubicMeter(x.MolesPerCubicMeter + y.MolesPerCubicMeter)));
        }
Exemplo n.º 24
0
        public string ToString(MolarityUnit 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));
        }
Exemplo n.º 25
0
        public string ToString(MolarityUnit 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));
        }
Exemplo n.º 26
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MolarityUnit unit)
        {
            return(unit switch
            {
                MolarityUnit.CentimolePerLiter => (CentimolesPerLiterInOneMolesPerCubicMeter, CentimolesPerLiterTolerance),
                MolarityUnit.CentimolesPerLiter => (CentimolesPerLiterInOneMolesPerCubicMeter, CentimolesPerLiterTolerance),
                MolarityUnit.DecimolePerLiter => (DecimolesPerLiterInOneMolesPerCubicMeter, DecimolesPerLiterTolerance),
                MolarityUnit.DecimolesPerLiter => (DecimolesPerLiterInOneMolesPerCubicMeter, DecimolesPerLiterTolerance),
                MolarityUnit.FemtomolePerLiter => (FemtomolesPerLiterInOneMolesPerCubicMeter, FemtomolesPerLiterTolerance),
                MolarityUnit.MicromolePerLiter => (MicromolesPerLiterInOneMolesPerCubicMeter, MicromolesPerLiterTolerance),
                MolarityUnit.MicromolesPerLiter => (MicromolesPerLiterInOneMolesPerCubicMeter, MicromolesPerLiterTolerance),
                MolarityUnit.MillimolePerLiter => (MillimolesPerLiterInOneMolesPerCubicMeter, MillimolesPerLiterTolerance),
                MolarityUnit.MillimolesPerLiter => (MillimolesPerLiterInOneMolesPerCubicMeter, MillimolesPerLiterTolerance),
                MolarityUnit.MolePerCubicMeter => (MolesPerCubicMeterInOneMolesPerCubicMeter, MolesPerCubicMeterTolerance),
                MolarityUnit.MolePerLiter => (MolesPerLiterInOneMolesPerCubicMeter, MolesPerLiterTolerance),
                MolarityUnit.MolesPerCubicMeter => (MolesPerCubicMeterInOneMolesPerCubicMeter, MolesPerCubicMeterTolerance),
                MolarityUnit.MolesPerLiter => (MolesPerLiterInOneMolesPerCubicMeter, MolesPerLiterTolerance),
                MolarityUnit.NanomolePerLiter => (NanomolesPerLiterInOneMolesPerCubicMeter, NanomolesPerLiterTolerance),
                MolarityUnit.NanomolesPerLiter => (NanomolesPerLiterInOneMolesPerCubicMeter, NanomolesPerLiterTolerance),
                MolarityUnit.PicomolePerLiter => (PicomolesPerLiterInOneMolesPerCubicMeter, PicomolesPerLiterTolerance),
                MolarityUnit.PicomolesPerLiter => (PicomolesPerLiterInOneMolesPerCubicMeter, PicomolesPerLiterTolerance),
                _ => throw new NotSupportedException()
            });
Exemplo n.º 27
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="MolarityUnit" /> to <see cref="Molarity" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>Molarity unit value.</returns>
        public static Molarity?From(double?value, MolarityUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case MolarityUnit.CentimolesPerLiter:
                return(FromCentimolesPerLiter(value.Value));

            case MolarityUnit.DecimolesPerLiter:
                return(FromDecimolesPerLiter(value.Value));

            case MolarityUnit.MicromolesPerLiter:
                return(FromMicromolesPerLiter(value.Value));

            case MolarityUnit.MillimolesPerLiter:
                return(FromMillimolesPerLiter(value.Value));

            case MolarityUnit.MolesPerCubicMeter:
                return(FromMolesPerCubicMeter(value.Value));

            case MolarityUnit.MolesPerLiter:
                return(FromMolesPerLiter(value.Value));

            case MolarityUnit.NanomolesPerLiter:
                return(FromNanomolesPerLiter(value.Value));

            case MolarityUnit.PicomolesPerLiter:
                return(FromPicomolesPerLiter(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Exemplo n.º 28
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(MolarityUnit unit, [CanBeNull] Culture culture)
 {
     return(ToString(unit, culture, 2));
 }
Exemplo n.º 29
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(MolarityUnit unit)
 {
     return(ToString(unit, null, 2));
 }
Exemplo n.º 30
0
 public static string GetAbbreviation(MolarityUnit unit, [CanBeNull] Culture culture)
 {
     return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit));
 }