Exemple #1
0
        public string ToString(TorqueUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, culture, format));
        }
Exemple #2
0
        public static string GetAbbreviation(TorqueUnit 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));
        }
Exemple #3
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="TorqueUnit" /> to <see cref="Torque" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>Torque unit value.</returns>
        public static Torque?From(double?value, TorqueUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case TorqueUnit.KilogramForceCentimeter:
                return(FromKilogramForceCentimeters(value.Value));

            case TorqueUnit.KilogramForceMeter:
                return(FromKilogramForceMeters(value.Value));

            case TorqueUnit.KilogramForceMillimeter:
                return(FromKilogramForceMillimeters(value.Value));

            case TorqueUnit.KilonewtonCentimeter:
                return(FromKilonewtonCentimeters(value.Value));

            case TorqueUnit.KilonewtonMeter:
                return(FromKilonewtonMeters(value.Value));

            case TorqueUnit.KilonewtonMillimeter:
                return(FromKilonewtonMillimeters(value.Value));

            case TorqueUnit.KilopoundForceFoot:
                return(FromKilopoundForceFeet(value.Value));

            case TorqueUnit.KilopoundForceInch:
                return(FromKilopoundForceInches(value.Value));

            case TorqueUnit.NewtonCentimeter:
                return(FromNewtonCentimeters(value.Value));

            case TorqueUnit.NewtonMeter:
                return(FromNewtonMeters(value.Value));

            case TorqueUnit.NewtonMillimeter:
                return(FromNewtonMillimeters(value.Value));

            case TorqueUnit.PoundForceFoot:
                return(FromPoundForceFeet(value.Value));

            case TorqueUnit.PoundForceInch:
                return(FromPoundForceInches(value.Value));

            case TorqueUnit.TonneForceCentimeter:
                return(FromTonneForceCentimeters(value.Value));

            case TorqueUnit.TonneForceMeter:
                return(FromTonneForceMeters(value.Value));

            case TorqueUnit.TonneForceMillimeter:
                return(FromTonneForceMillimeters(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Exemple #4
0
        private double GetValueAs(TorqueUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = GetValueInBaseUnit();

            switch (unit)
            {
            case TorqueUnit.KilogramForceCentimeter: return(baseUnitValue * 10.1971619222242);

            case TorqueUnit.KilogramForceMeter: return(baseUnitValue * 0.101971619222242);

            case TorqueUnit.KilogramForceMillimeter: return(baseUnitValue * 101.971619222242);

            case TorqueUnit.KilonewtonCentimeter: return((baseUnitValue * 100) / 1e3d);

            case TorqueUnit.KilonewtonMeter: return((baseUnitValue) / 1e3d);

            case TorqueUnit.KilonewtonMillimeter: return((baseUnitValue * 1000) / 1e3d);

            case TorqueUnit.KilopoundForceFoot: return((baseUnitValue / 1.3558179483314) / 1e3d);

            case TorqueUnit.KilopoundForceInch: return((baseUnitValue / 1.129848290276167e-1) / 1e3d);

            case TorqueUnit.MeganewtonCentimeter: return((baseUnitValue * 100) / 1e6d);

            case TorqueUnit.MeganewtonMeter: return((baseUnitValue) / 1e6d);

            case TorqueUnit.MeganewtonMillimeter: return((baseUnitValue * 1000) / 1e6d);

            case TorqueUnit.MegapoundForceFoot: return((baseUnitValue / 1.3558179483314) / 1e6d);

            case TorqueUnit.MegapoundForceInch: return((baseUnitValue / 1.129848290276167e-1) / 1e6d);

            case TorqueUnit.NewtonCentimeter: return(baseUnitValue * 100);

            case TorqueUnit.NewtonMeter: return(baseUnitValue);

            case TorqueUnit.NewtonMillimeter: return(baseUnitValue * 1000);

            case TorqueUnit.PoundalFoot: return(baseUnitValue / 4.21401100938048e-2);

            case TorqueUnit.PoundForceFoot: return(baseUnitValue / 1.3558179483314);

            case TorqueUnit.PoundForceInch: return(baseUnitValue / 1.129848290276167e-1);

            case TorqueUnit.TonneForceCentimeter: return(baseUnitValue * 0.0101971619222242);

            case TorqueUnit.TonneForceMeter: return(baseUnitValue * 0.000101971619222242);

            case TorqueUnit.TonneForceMillimeter: return(baseUnitValue * 0.101971619222242);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Exemple #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            int        unit = ((dynamic)ddl_unit.SelectedItem).code;
            TorqueUnit t    = (TorqueUnit)Enum.Parse(typeof(Common.TorqueUnit), unit.ToString());

            byte[] cmd = Command.SetUnit(t);
            s.SendCmd(cmd);
        }
 public void TorqueConversions(double value1, TorqueUnit units1, double value2, TorqueUnit units2)
 {
     new Torque(value1, units1) {
         Units = units2
     }.Value.ShouldBeWithinEpsilonOf(value2);
     new Torque(value2, units2) {
         Units = units1
     }.Value.ShouldBeWithinEpsilonOf(value1);
 }
Exemple #7
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(TorqueUnit unit)
        {
            switch (unit)
            {
            case TorqueUnit.KilogramForceCentimeter:
                return(KilogramForceCentimeters);

            case TorqueUnit.KilogramForceMeter:
                return(KilogramForceMeters);

            case TorqueUnit.KilogramForceMillimeter:
                return(KilogramForceMillimeters);

            case TorqueUnit.KilonewtonCentimeter:
                return(KilonewtonCentimeters);

            case TorqueUnit.KilonewtonMeter:
                return(KilonewtonMeters);

            case TorqueUnit.KilonewtonMillimeter:
                return(KilonewtonMillimeters);

            case TorqueUnit.KilopoundForceFoot:
                return(KilopoundForceFeet);

            case TorqueUnit.KilopoundForceInch:
                return(KilopoundForceInches);

            case TorqueUnit.NewtonCentimeter:
                return(NewtonCentimeters);

            case TorqueUnit.NewtonMeter:
                return(NewtonMeters);

            case TorqueUnit.NewtonMillimeter:
                return(NewtonMillimeters);

            case TorqueUnit.PoundForceFoot:
                return(PoundForceFeet);

            case TorqueUnit.PoundForceInch:
                return(PoundForceInches);

            case TorqueUnit.TonneForceCentimeter:
                return(TonneForceCentimeters);

            case TorqueUnit.TonneForceMeter:
                return(TonneForceMeters);

            case TorqueUnit.TonneForceMillimeter:
                return(TonneForceMillimeters);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
Exemple #8
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 Torque(decimal value, TorqueUnit unit)
        {
            if (unit == TorqueUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
Exemple #9
0
        public string ToString(TorqueUnit unit, CultureInfo culture, string format, params object[] args)
        {
            string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);

            object[] finalArgs = new object[] { As(unit), abbreviation }
            .Concat(args)
            .ToArray();

            return(string.Format(culture, format, finalArgs));
        }
Exemple #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 Torque(double value, TorqueUnit unit)
        {
            if (unit == TorqueUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
Exemple #11
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="TorqueUnit" /> to <see cref="Torque" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>Torque unit value.</returns>
        public static Torque From(double value, TorqueUnit fromUnit)
        {
            switch (fromUnit)
            {
            case TorqueUnit.Newtonmeter:
                return(FromNewtonmeters(value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Exemple #12
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(TorqueUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
Exemple #13
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(TorqueUnit unit)
        {
            switch (unit)
            {
            case TorqueUnit.Newtonmeter:
                return(Newtonmeters);

            default:
                throw new NotImplementedException("unit: " + unit);
            }
        }
Exemple #14
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(TorqueUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Exemple #15
0
        /// <summary>
        /// 设置扭矩单位
        /// cmd:N.m:06,kgf•cm:02,
        /// kgf•m:03,lbf•in:08,lbf•ft:09
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static byte[] SetUnit(TorqueUnit cmd)
        {
            var           val = Convert.ToInt32(cmd).ToString().PadLeft(2, '0');
            StringBuilder sb  = new StringBuilder();

            sb.Append("AT008");
            sb.Append(",");
            sb.Append(val);
            sb.Append("\r\n");
            return(Encoding.ASCII.GetBytes(sb.ToString()));
        }
Exemple #16
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 Torque 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
            {
                TorqueUnit 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;
            }
        }
Exemple #17
0
        /// <summary>
        ///     Parse a string given a particular regular expression.
        /// </summary>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        private static List <Torque> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null)
        {
            var             regex     = new Regex(regexString);
            MatchCollection matches   = regex.Matches(str.Trim());
            var             converted = new List <Torque>();

            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
                {
                    TorqueUnit unit  = ParseUnit(unitString, formatProvider);
                    double     value = double.Parse(valueString, formatProvider);

                    converted.Add(From(value, unit));
                }
                catch (AmbiguousUnitParseException ambiguousException)
                {
                    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);
        }
Exemple #18
0
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="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 Torque Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <Torque, TorqueUnit>(str, provider,
                                                             delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TorqueUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtonMeters(x.NewtonMeters + y.NewtonMeters)));
        }
Exemple #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="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 Torque 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 <Torque, TorqueUnit>(str, provider,
                                                             delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TorqueUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtonMeters(x.NewtonMeters + y.NewtonMeters)));
        }
Exemple #20
0
        public string ToString(TorqueUnit 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));
        }
Exemple #21
0
        public string ToString(TorqueUnit 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));
        }
Exemple #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 Torque 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 <Torque>(str, formatProvider,
                                                 delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TorqueUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtonMeters(x.NewtonMeters + y.NewtonMeters)));
        }
Exemple #23
0
        public string ToString(TorqueUnit 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));
        }
Exemple #24
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 Torque 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 <Torque, TorqueUnit>(str, formatProvider,
                                                             delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                TorqueUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtonMeters(x.NewtonMeters + y.NewtonMeters)));
        }
Exemple #25
0
        public string ToString(TorqueUnit 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));
        }
Exemple #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(TorqueUnit unit)
 {
     return(ToString(unit, null, 2));
 }
Exemple #27
0
        /// <summary>
        ///     Converts this Torque to another Torque with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Torque with the specified unit.</returns>
        public Torque ToUnit(TorqueUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new Torque(convertedValue, unit));
        }
 protected static string CreateSuffix(SymbolFormat format, TorqueUnit unit)
 {
     return default(Torque).ToString(unit, format).Trim('0');
 }
Exemple #29
0
 public static string GetAbbreviation(TorqueUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
Exemple #30
0
        public static Torque From(QuantityValue value, TorqueUnit fromUnit)
#endif
        {
            return(new Torque((double)value, fromUnit));
        }
Exemple #31
0
 public static Torque From(double value, TorqueUnit fromUnit)
Exemple #32
0
 Torque(double numericValue, TorqueUnit unit)
 {
     _value = numericValue;
     _unit  = unit;
 }