예제 #1
0
 public UnitSystem()
 {
     this.temperature        = TemperatureUnit.Celsius;
     this.pressure           = PressureUnit.Pascal;
     this.massFlowRate       = MassFlowRateUnit.KgPerSec;
     this.volumeFlowRate     = VolumeFlowRateUnit.CubicMeterPerSec;
     this.moistureContent    = MoistureContentUnit.KgPerKg;
     this.relativeHumidity   = RatioUnit.Percent;
     this.enthalpy           = SpecificEnergyUnit.JoulePerKg;
     this.specificHeat       = SpecificHeatUnit.JoulePerKgKelvin;
     this.energy             = EnergyUnit.Joule;
     this.power              = PowerUnit.JoulePerSec;
     this.density            = DensityUnit.KgPerCubicMeter;
     this.dynamicViscosity   = DynamicViscosityUnit.PascalSecond;
     this.kinematicViscosity = KinematicViscosityUnit.SquareMeterPerSec;
     this.conductivity       = ThermalConductivityUnit.WattPerMeterKelvin;
     this.diffusivity        = DiffusivityUnit.SquareMeterPerSec;
     this.mass     = MassUnit.Kilogram;
     this.length   = LengthUnit.Meter;
     this.area     = AreaUnit.SquareMeter;
     this.volume   = VolumeUnit.CubicMeter;
     this.time     = TimeUnit.Second;
     this.Name     = "SI"; //ep
     this.readOnly = true;
 }
        private double AsBaseNumericType(KinematicViscosityUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case KinematicViscosityUnit.Centistokes: return((baseUnitValue * 1e4) / 1e-2d);

            case KinematicViscosityUnit.Decistokes: return((baseUnitValue * 1e4) / 1e-1d);

            case KinematicViscosityUnit.Kilostokes: return((baseUnitValue * 1e4) / 1e3d);

            case KinematicViscosityUnit.Microstokes: return((baseUnitValue * 1e4) / 1e-6d);

            case KinematicViscosityUnit.Millistokes: return((baseUnitValue * 1e4) / 1e-3d);

            case KinematicViscosityUnit.Nanostokes: return((baseUnitValue * 1e4) / 1e-9d);

            case KinematicViscosityUnit.SquareMeterPerSecond: return(baseUnitValue);

            case KinematicViscosityUnit.Stokes: return(baseUnitValue * 1e4);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
예제 #3
0
        public static string GetAbbreviation(KinematicViscosityUnit 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));
        }
예제 #4
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(KinematicViscosityUnit unit)
        {
            switch (unit)
            {
            case KinematicViscosityUnit.Centistokes:
                return(Centistokes);

            case KinematicViscosityUnit.Decistokes:
                return(Decistokes);

            case KinematicViscosityUnit.Kilostokes:
                return(Kilostokes);

            case KinematicViscosityUnit.Microstokes:
                return(Microstokes);

            case KinematicViscosityUnit.Millistokes:
                return(Millistokes);

            case KinematicViscosityUnit.Nanostokes:
                return(Nanostokes);

            case KinematicViscosityUnit.SquareMeterPerSecond:
                return(SquareMetersPerSecond);

            case KinematicViscosityUnit.Stokes:
                return(Stokes);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
예제 #5
0
        //HeatFluxDensityUnit heatTransferCoefficient;
        //ForceUnit force;
        //SurfaceTensionUnit surfaceTension;

        public UnitSystem(TemperatureUnit temperature, PressureUnit pressure, MassFlowRateUnit massFlowRate,
                          VolumeFlowRateUnit volumeFlowRate, MoistureContentUnit moistureContent,
                          RatioUnit relativeHumidity, SpecificEnergyUnit enthalpy,
                          SpecificHeatUnit specificHeat, EnergyUnit energy, PowerUnit power, DensityUnit density,
                          DynamicViscosityUnit dynamicViscosity, KinematicViscosityUnit kinematicViscosity,
                          ThermalConductivityUnit conductivity, DiffusivityUnit diffusivity, MassUnit mass,
                          LengthUnit length, AreaUnit area, VolumeUnit volume, TimeUnit Time, string name) //ep
        {
            this.temperature        = temperature;
            this.pressure           = pressure;
            this.massFlowRate       = massFlowRate;
            this.volumeFlowRate     = volumeFlowRate;
            this.moistureContent    = moistureContent;
            this.relativeHumidity   = relativeHumidity;
            this.enthalpy           = enthalpy;
            this.specificHeat       = specificHeat;
            this.energy             = energy;
            this.power              = power;
            this.density            = density;
            this.dynamicViscosity   = dynamicViscosity;
            this.kinematicViscosity = kinematicViscosity;
            this.conductivity       = conductivity;
            this.diffusivity        = diffusivity;
            this.mass     = mass;
            this.length   = length;
            this.area     = area;
            this.volume   = volume;
            this.time     = time;
            this.Name     = name;  //ep
            this.readOnly = false; //ep
        }
예제 #6
0
        public string ToString(KinematicViscosityUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, culture, format));
        }
예제 #7
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="KinematicViscosityUnit" /> to <see cref="KinematicViscosity" />.
        /// </summary>
        /// <param name="val">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>KinematicViscosity unit value.</returns>
        public static KinematicViscosity From(double val, KinematicViscosityUnit fromUnit)
        {
            switch (fromUnit)
            {
            case KinematicViscosityUnit.Centistokes:
                return(FromCentistokes(val));

            case KinematicViscosityUnit.Decistokes:
                return(FromDecistokes(val));

            case KinematicViscosityUnit.Kilostokes:
                return(FromKilostokes(val));

            case KinematicViscosityUnit.Microstokes:
                return(FromMicrostokes(val));

            case KinematicViscosityUnit.Millistokes:
                return(FromMillistokes(val));

            case KinematicViscosityUnit.Nanostokes:
                return(FromNanostokes(val));

            case KinematicViscosityUnit.SquareMeterPerSecond:
                return(FromSquareMetersPerSecond(val));

            case KinematicViscosityUnit.Stokes:
                return(FromStokes(val));

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

            return(new KinematicViscosity((double)value.Value, fromUnit));
        }
예제 #9
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 KinematicViscosity(decimal value, KinematicViscosityUnit unit)
        {
            if (unit == KinematicViscosityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
예제 #10
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 KinematicViscosity(double value, KinematicViscosityUnit unit)
        {
            if (unit == KinematicViscosityUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

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

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
예제 #12
0
        /// <summary>
        ///     Parse a string of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <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 2 words. Input string needs to be in the format "&lt;quantity&gt; &lt;unit
        ///     &gt;".
        /// </exception>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        public static KinematicViscosity Parse(string str, IFormatProvider formatProvider = null)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }

            var numFormat = formatProvider != null ?
                            (NumberFormatInfo)formatProvider.GetFormat(typeof(NumberFormatInfo)) :
                            NumberFormatInfo.CurrentInfo;

            var numRegex = string.Format(@"[\d., {0}{1}]*\d",               // allows digits, dots, commas, and spaces in the quantity (must end in digit)
                                         numFormat.NumberGroupSeparator,    // adds provided (or current) culture's group separator
                                         numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator
            var regexString = string.Format("(?<value>[-+]?{0}{1}{2}{3}",
                                            numRegex,                       // capture base (integral) Quantity value
                                            @"(?:[eE][-+]?\d+)?)",          // capture exponential (if any), end of Quantity capturing
                                            @"\s?",                         // ignore whitespace (allows both "1kg", "1 kg")
                                            @"(?<unit>\S+)");               // capture Unit (non-whitespace) input

            var             regex  = new Regex(regexString);
            GroupCollection groups = regex.Match(str.Trim()).Groups;

            var valueString = groups["value"].Value;
            var unitString  = groups["unit"].Value;

            if (valueString == "" || unitString == "")
            {
                var ex = new ArgumentException(
                    "Expected valid quantity and unit. Input string needs to be in the format \"<quantity><unit> or <quantity> <unit>\".", "str");
                ex.Data["input"]          = str;
                ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                throw ex;
            }

            try
            {
                KinematicViscosityUnit unit = ParseUnit(unitString, formatProvider);
                double value = double.Parse(valueString, formatProvider);

                return(From(value, unit));
            }
            catch (Exception e)
            {
                var newEx = new UnitsNetException("Error parsing string.", e);
                newEx.Data["input"]          = str;
                newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                throw newEx;
            }
        }
예제 #13
0
        /// <summary>
        ///     Parse a string given a particular regular expression.
        /// </summary>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        private static List <KinematicViscosity> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null)
        {
            var             regex     = new Regex(regexString);
            MatchCollection matches   = regex.Matches(str.Trim());
            var             converted = new List <KinematicViscosity>();

            foreach (Match match in matches)
            {
                GroupCollection groups = match.Groups;

                var valueString = groups["value"].Value;
                var unitString  = groups["unit"].Value;
                if (groups["invalid"].Value != "")
                {
                    var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value);
                    newEx.Data["input"]          = str;
                    newEx.Data["matched value"]  = valueString;
                    newEx.Data["matched unit"]   = unitString;
                    newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                    throw newEx;
                }
                if (valueString == "" && unitString == "")
                {
                    continue;
                }

                try
                {
                    KinematicViscosityUnit unit = ParseUnit(unitString, formatProvider);
                    double value = double.Parse(valueString, formatProvider);

                    converted.Add(From(value, unit));
                }
                catch (AmbiguousUnitParseException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    var newEx = new UnitsNetException("Error parsing string.", ex);
                    newEx.Data["input"]          = str;
                    newEx.Data["matched value"]  = valueString;
                    newEx.Data["matched unit"]   = unitString;
                    newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                    throw newEx;
                }
            }
            return(converted);
        }
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(KinematicViscosityUnit unit)
        {
            return(unit switch
            {
                KinematicViscosityUnit.Centistokes => (CentistokesInOneSquareMeterPerSecond, CentistokesTolerance),
                KinematicViscosityUnit.Decistokes => (DecistokesInOneSquareMeterPerSecond, DecistokesTolerance),
                KinematicViscosityUnit.Kilostokes => (KilostokesInOneSquareMeterPerSecond, KilostokesTolerance),
                KinematicViscosityUnit.Microstokes => (MicrostokesInOneSquareMeterPerSecond, MicrostokesTolerance),
                KinematicViscosityUnit.Millistokes => (MillistokesInOneSquareMeterPerSecond, MillistokesTolerance),
                KinematicViscosityUnit.Nanostokes => (NanostokesInOneSquareMeterPerSecond, NanostokesTolerance),
                KinematicViscosityUnit.SquareFootPerSecond => (SquareFeetPerSecondInOneSquareMeterPerSecond, SquareFeetPerSecondTolerance),
                KinematicViscosityUnit.SquareMeterPerSecond => (SquareMetersPerSecondInOneSquareMeterPerSecond, SquareMetersPerSecondTolerance),
                KinematicViscosityUnit.Stokes => (StokesInOneSquareMeterPerSecond, StokesTolerance),
                _ => throw new NotSupportedException()
            });
        /// <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 KinematicViscosity Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <KinematicViscosity, KinematicViscosityUnit>(str, provider,
                                                                                     delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                KinematicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromSquareMetersPerSecond(x.SquareMetersPerSecond + y.SquareMetersPerSecond)));
        }
예제 #16
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 KinematicViscosity 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 <KinematicViscosity, KinematicViscosityUnit>(str, provider,
                                                                                     delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                KinematicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromSquareMetersPerSecond(x.SquareMetersPerSecond + y.SquareMetersPerSecond)));
        }
        public string ToString(KinematicViscosityUnit 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));
        }
예제 #18
0
        public string ToString(KinematicViscosityUnit 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));
        }
예제 #19
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 KinematicViscosity 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 <KinematicViscosity>(str, formatProvider,
                                                             delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                KinematicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromSquareMetersPerSecond(x.SquareMetersPerSecond + y.SquareMetersPerSecond)));
        }
예제 #20
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 KinematicViscosity 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 <KinematicViscosity, KinematicViscosityUnit>(str, formatProvider,
                                                                                     delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                KinematicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromSquareMetersPerSecond(x.SquareMetersPerSecond + y.SquareMetersPerSecond)));
        }
예제 #21
0
        public string ToString(KinematicViscosityUnit 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));
        }
예제 #22
0
        public string ToString(KinematicViscosityUnit 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));
        }
예제 #23
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="KinematicViscosityUnit" /> to <see cref="KinematicViscosity" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>KinematicViscosity unit value.</returns>
        public static KinematicViscosity?From(QuantityValue?value, KinematicViscosityUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case KinematicViscosityUnit.Centistokes:
                return(FromCentistokes(value.Value));

            case KinematicViscosityUnit.Decistokes:
                return(FromDecistokes(value.Value));

            case KinematicViscosityUnit.Kilostokes:
                return(FromKilostokes(value.Value));

            case KinematicViscosityUnit.Microstokes:
                return(FromMicrostokes(value.Value));

            case KinematicViscosityUnit.Millistokes:
                return(FromMillistokes(value.Value));

            case KinematicViscosityUnit.Nanostokes:
                return(FromNanostokes(value.Value));

            case KinematicViscosityUnit.SquareMeterPerSecond:
                return(FromSquareMetersPerSecond(value.Value));

            case KinematicViscosityUnit.Stokes:
                return(FromStokes(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
 protected static string CreateSuffix(SymbolFormat format, KinematicViscosityUnit unit)
 {
     return default(KinematicViscosity).ToString(unit, format).Trim('0');
 }
예제 #25
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(KinematicViscosityUnit unit, [CanBeNull] Culture culture)
 {
     return(ToString(unit, culture, 2));
 }
예제 #26
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(KinematicViscosityUnit unit)
 {
     return(ToString(unit, null, 2));
 }
 public static KinematicViscosity From(double value, KinematicViscosityUnit fromUnit)
예제 #28
0
 public static string GetAbbreviation(KinematicViscosityUnit unit, [CanBeNull] Culture culture)
 {
     return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit));
 }
예제 #29
0
 public static string GetAbbreviation(KinematicViscosityUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
        public static KinematicViscosity From(QuantityValue value, KinematicViscosityUnit fromUnit)
#endif
        {
            return(new KinematicViscosity((double)value, fromUnit));
        }
        /// <summary>
        ///     Converts this KinematicViscosity to another KinematicViscosity with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A KinematicViscosity with the specified unit.</returns>
        public KinematicViscosity ToUnit(KinematicViscosityUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new KinematicViscosity(convertedValue, unit));
        }