Esempio n. 1
0
        public string ToString(LinearDensityUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, cultureName, format));
        }
Esempio n. 2
0
        public string ToString(LinearDensityUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, provider, format));
        }
Esempio n. 3
0
        public static string GetAbbreviation(LinearDensityUnit 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));
        }
Esempio 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="LinearDensityUnit" /> to <see cref="LinearDensity" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>LinearDensity unit value.</returns>
        public static LinearDensity?From(QuantityValue?value, LinearDensityUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }

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

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

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = unit;
        }
Esempio n. 7
0
        public void T01_should_decompose_kg_per_meter()
        {
            var d = new LinearDensityUnit(MassUnits.Kg, LengthUnits.Meter);
            var q = d.Decompose();

            Assert.Equal(2, q.Count);
            Assert.Equal("kg", q[0].Unit.UnitName);
            Assert.Equal(1, q[0].Power);
            Assert.Equal("m", q[1].Unit.UnitName);
            Assert.Equal(-1, q[1].Power);
        }
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(LinearDensityUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Esempio n. 9
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 LinearDensity Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <LinearDensity, LinearDensityUnit>(str, provider,
                                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                LinearDensityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKilogramsPerMeter(x.KilogramsPerMeter + y.KilogramsPerMeter)));
        }
Esempio n. 10
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(LinearDensityUnit unit)
        {
            switch (unit)
            {
            case LinearDensityUnit.GramPerMeter:
                return(GramsPerMeter);

            case LinearDensityUnit.KilogramPerMeter:
                return(KilogramsPerMeter);

            case LinearDensityUnit.PoundPerFoot:
                return(PoundsPerFoot);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
Esempio n. 11
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 LinearDensity 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 <LinearDensity, LinearDensityUnit>(str, provider,
                                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                LinearDensityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKilogramsPerMeter(x.KilogramsPerMeter + y.KilogramsPerMeter)));
        }
Esempio n. 12
0
        public string ToString(LinearDensityUnit 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));
        }
Esempio n. 13
0
        public static LinearDensity From(QuantityValue value, LinearDensityUnit fromUnit)
#endif
        {
            switch (fromUnit)
            {
            case LinearDensityUnit.GramPerMeter:
                return(FromGramsPerMeter(value));

            case LinearDensityUnit.KilogramPerMeter:
                return(FromKilogramsPerMeter(value));

            case LinearDensityUnit.PoundPerFoot:
                return(FromPoundsPerFoot(value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Esempio n. 14
0
        private double AsBaseNumericType(LinearDensityUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case LinearDensityUnit.GramPerCentimeter: return(baseUnitValue / 1e-1);

            case LinearDensityUnit.GramPerMeter: return(baseUnitValue / 1e-3);

            case LinearDensityUnit.GramPerMillimeter: return(baseUnitValue);

            case LinearDensityUnit.KilogramPerCentimeter: return((baseUnitValue / 1e-1) / 1e3d);

            case LinearDensityUnit.KilogramPerMeter: return((baseUnitValue / 1e-3) / 1e3d);

            case LinearDensityUnit.KilogramPerMillimeter: return((baseUnitValue) / 1e3d);

            case LinearDensityUnit.MicrogramPerCentimeter: return((baseUnitValue / 1e-1) / 1e-6d);

            case LinearDensityUnit.MicrogramPerMeter: return((baseUnitValue / 1e-3) / 1e-6d);

            case LinearDensityUnit.MicrogramPerMillimeter: return((baseUnitValue) / 1e-6d);

            case LinearDensityUnit.MilligramPerCentimeter: return((baseUnitValue / 1e-1) / 1e-3d);

            case LinearDensityUnit.MilligramPerMeter: return((baseUnitValue / 1e-3) / 1e-3d);

            case LinearDensityUnit.MilligramPerMillimeter: return((baseUnitValue) / 1e-3d);

            case LinearDensityUnit.PoundPerFoot: return(baseUnitValue / 1.48816394);

            case LinearDensityUnit.PoundPerInch: return(baseUnitValue * 5.5997415e-2);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Esempio n. 15
0
        public string ToString(LinearDensityUnit 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(LinearDensityUnit unit)
        {
            return(unit switch
            {
                LinearDensityUnit.GramPerCentimeter => (GramsPerCentimeterInOneKilogramPerMeter, GramsPerCentimeterTolerance),
                LinearDensityUnit.GramPerMeter => (GramsPerMeterInOneKilogramPerMeter, GramsPerMeterTolerance),
                LinearDensityUnit.GramPerMillimeter => (GramsPerMillimeterInOneKilogramPerMeter, GramsPerMillimeterTolerance),
                LinearDensityUnit.KilogramPerCentimeter => (KilogramsPerCentimeterInOneKilogramPerMeter, KilogramsPerCentimeterTolerance),
                LinearDensityUnit.KilogramPerMeter => (KilogramsPerMeterInOneKilogramPerMeter, KilogramsPerMeterTolerance),
                LinearDensityUnit.KilogramPerMillimeter => (KilogramsPerMillimeterInOneKilogramPerMeter, KilogramsPerMillimeterTolerance),
                LinearDensityUnit.MicrogramPerCentimeter => (MicrogramsPerCentimeterInOneKilogramPerMeter, MicrogramsPerCentimeterTolerance),
                LinearDensityUnit.MicrogramPerMeter => (MicrogramsPerMeterInOneKilogramPerMeter, MicrogramsPerMeterTolerance),
                LinearDensityUnit.MicrogramPerMillimeter => (MicrogramsPerMillimeterInOneKilogramPerMeter, MicrogramsPerMillimeterTolerance),
                LinearDensityUnit.MilligramPerCentimeter => (MilligramsPerCentimeterInOneKilogramPerMeter, MilligramsPerCentimeterTolerance),
                LinearDensityUnit.MilligramPerMeter => (MilligramsPerMeterInOneKilogramPerMeter, MilligramsPerMeterTolerance),
                LinearDensityUnit.MilligramPerMillimeter => (MilligramsPerMillimeterInOneKilogramPerMeter, MilligramsPerMillimeterTolerance),
                LinearDensityUnit.PoundPerFoot => (PoundsPerFootInOneKilogramPerMeter, PoundsPerFootTolerance),
                LinearDensityUnit.PoundPerInch => (PoundsPerInchInOneKilogramPerMeter, PoundsPerInchTolerance),
                _ => throw new NotSupportedException()
            });
Esempio 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="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 LinearDensity 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 <LinearDensity, LinearDensityUnit>(str, formatProvider,
                                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                LinearDensityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromKilogramsPerMeter(x.KilogramsPerMeter + y.KilogramsPerMeter)));
        }
Esempio n. 18
0
        public string ToString(LinearDensityUnit 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));
        }
Esempio n. 19
0
        public static string GetAbbreviation(
            LinearDensityUnit unit,
#if WINDOWS_UWP
            [CanBeNull] string cultureName)
        /// <summary>
        ///     Converts this LinearDensity to another LinearDensity with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A LinearDensity with the specified unit.</returns>
        public LinearDensity ToUnit(LinearDensityUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new LinearDensity(convertedValue, unit));
        }
        /// <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 LinearDensityUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <LinearDensityUnit>(str, provider, out unit));
        }
 public static bool TryParseUnit(string str, out LinearDensityUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
 public static LinearDensity From(double value, LinearDensityUnit fromUnit)
 {
     return(new LinearDensity((double)value, fromUnit));
 }
        /// <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(LinearDensityUnit 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(LinearDensityUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
Esempio n. 26
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(LinearDensityUnit unit, [CanBeNull] IFormatProvider provider)
 {
     return(ToString(unit, provider, 2));
 }
Esempio n. 27
0
        public static LinearDensity From(QuantityValue value, LinearDensityUnit fromUnit)
#endif
        {
            return(new LinearDensity((double)value, fromUnit));
        }
Esempio n. 28
0
 public static LinearDensity From(double value, LinearDensityUnit fromUnit)
Esempio n. 29
0
 LinearDensity(double numericValue, LinearDensityUnit unit)
 {
     _value = numericValue;
     _unit  = unit;
 }
Esempio n. 30
0
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="LinearDensityUnit" /> to <see cref="LinearDensity" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>LinearDensity unit value.</returns>
 public static LinearDensity?From(QuantityValue?value, LinearDensityUnit fromUnit)
 {
     return(value.HasValue ? new LinearDensity((double)value.Value, fromUnit) : default(LinearDensity?));
 }