Пример #1
0
        public string ToString(ForceUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, provider, format));
        }
Пример #2
0
        private double AsBaseNumericType(ForceUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case ForceUnit.Decanewton: return((baseUnitValue) / 1e1d);

            case ForceUnit.Dyn: return(baseUnitValue * 1e5);

            case ForceUnit.KilogramForce: return(baseUnitValue / 9.80665002864);

            case ForceUnit.Kilonewton: return((baseUnitValue) / 1e3d);

            case ForceUnit.KiloPond: return(baseUnitValue / 9.80665002864);

            case ForceUnit.Meganewton: return((baseUnitValue) / 1e6d);

            case ForceUnit.Newton: return(baseUnitValue);

            case ForceUnit.Poundal: return(baseUnitValue / 0.13825502798973041652092282466083);

            case ForceUnit.PoundForce: return(baseUnitValue / 4.4482216152605095551842641431421);

            case ForceUnit.TonneForce: return(baseUnitValue / 9.80665002864e3);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Пример #3
0
        /// <summary>
        ///     Dynamically convert from value and unit enum <see cref="ForceUnit" /> to <see cref="Force" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>Force unit value.</returns>
        public static Force From(double value, ForceUnit fromUnit)
        {
            switch (fromUnit)
            {
            case ForceUnit.Dyn:
                return(FromDyne(value));

            case ForceUnit.KilogramForce:
                return(FromKilogramsForce(value));

            case ForceUnit.Kilonewton:
                return(FromKilonewtons(value));

            case ForceUnit.KiloPond:
                return(FromKiloPonds(value));

            case ForceUnit.Newton:
                return(FromNewtons(value));

            case ForceUnit.Poundal:
                return(FromPoundals(value));

            case ForceUnit.PoundForce:
                return(FromPoundForces(value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Пример #4
0
        public string ToString(ForceUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, culture, format));
        }
Пример #5
0
        public static string GetAbbreviation(ForceUnit 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));
        }
Пример #6
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(ForceUnit unit)
        {
            switch (unit)
            {
            case ForceUnit.Dyn:
                return(Dyne);

            case ForceUnit.KilogramForce:
                return(KilogramsForce);

            case ForceUnit.Kilonewton:
                return(Kilonewtons);

            case ForceUnit.KiloPond:
                return(KiloPonds);

            case ForceUnit.Newton:
                return(Newtons);

            case ForceUnit.Poundal:
                return(Poundals);

            case ForceUnit.PoundForce:
                return(PoundForces);

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

            return(new Force((double)value.Value, fromUnit));
        }
 public void ForceConversions(double value1, ForceUnit units1, double value2, ForceUnit units2)
 {
     new Force(value1, units1) {
         Units = units2
     }.Value.ShouldBeWithinEpsilonOf(value2);
     new Force(value2, units2) {
         Units = units1
     }.Value.ShouldBeWithinEpsilonOf(value1);
 }
Пример #9
0
 void Awake()
 {
     GetComponent <UIEventListener>().onClick = OnItemClick;
     SC_RankingUnit    = CreatObjectToNGUI.InstantiateObj(RankingUint_prefab, RankingUnitPoint).GetComponent <RankingUnit>();
     SC_PlayerHeadUnit = CreatObjectToNGUI.InstantiateObj(PlayerHeadUnit_prefab, PlayerHeadUnitPoint).GetComponent <PlayerHeadUnit>();
     SC_SirenListUnit  = CreatObjectToNGUI.InstantiateObj(SirenListUnit_prefab, SirenListUnitPoint).GetComponent <SirenListUnit>();
     SC_ForceUnit      = CreatObjectToNGUI.InstantiateObj(ForceUnit_prefab, ForceUnitPoint).GetComponent <ForceUnit>();
     LookdetailBtn.SetCallBackFuntion(DetailBtnClick);
 }
Пример #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 Force(decimal value, ForceUnit unit)
        {
            if (unit == ForceUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
Пример #11
0
        public string ToString(ForceUnit 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));
        }
Пример #12
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 Force(double value, ForceUnit unit)
        {
            if (unit == ForceUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));
            _unit  = unit;
        }
Пример #13
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(ForceUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
Пример #14
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(ForceUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
Пример #15
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 Force 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
            {
                ForceUnit 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;
            }
        }
Пример #16
0
        /// <summary>
        ///     Parse a string given a particular regular expression.
        /// </summary>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        private static List <Force> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null)
        {
            var             regex     = new Regex(regexString);
            MatchCollection matches   = regex.Matches(str.Trim());
            var             converted = new List <Force>();

            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
                {
                    ForceUnit 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);
        }
Пример #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="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 Force Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <Force, ForceUnit>(str, provider,
                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                ForceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtons(x.Newtons + y.Newtons)));
        }
Пример #18
0
        private double GetValueAs(ForceUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = GetValueInBaseUnit();

            switch (unit)
            {
            case ForceUnit.Decanewton: return((baseUnitValue) / 1e1d);

            case ForceUnit.Dyn: return(baseUnitValue * 1e5);

            case ForceUnit.KilogramForce: return(baseUnitValue / 9.80665002864);

            case ForceUnit.Kilonewton: return((baseUnitValue) / 1e3d);

            case ForceUnit.KiloPond: return(baseUnitValue / 9.80665002864);

            case ForceUnit.KilopoundForce: return((baseUnitValue / 4.4482216152605095551842641431421) / 1e3d);

            case ForceUnit.Meganewton: return((baseUnitValue) / 1e6d);

            case ForceUnit.Micronewton: return((baseUnitValue) / 1e-6d);

            case ForceUnit.Millinewton: return((baseUnitValue) / 1e-3d);

            case ForceUnit.Newton: return(baseUnitValue);

            case ForceUnit.OunceForce: return(baseUnitValue / 2.780138509537812e-1);

            case ForceUnit.Poundal: return(baseUnitValue / 0.13825502798973041652092282466083);

            case ForceUnit.PoundForce: return(baseUnitValue / 4.4482216152605095551842641431421);

            case ForceUnit.ShortTonForce: return(baseUnitValue / 8.896443230521e3);

            case ForceUnit.TonneForce: return(baseUnitValue / 9.80665002864e3);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
Пример #19
0
        public string ToString(ForceUnit 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));
        }
Пример #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="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 Force 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 <Force, ForceUnit>(str, provider,
                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                ForceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtons(x.Newtons + y.Newtons)));
        }
Пример #21
0
        public string ToString(ForceUnit 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));
        }
Пример #22
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="ForceUnit" /> to <see cref="Force" />.
        /// </summary>
        /// <param name="value">Value to convert from.</param>
        /// <param name="fromUnit">Unit to convert from.</param>
        /// <returns>Force unit value.</returns>
        public static Force?From(QuantityValue?value, ForceUnit fromUnit)
        {
            if (!value.HasValue)
            {
                return(null);
            }
            switch (fromUnit)
            {
            case ForceUnit.Decanewton:
                return(FromDecanewtons(value.Value));

            case ForceUnit.Dyn:
                return(FromDyne(value.Value));

            case ForceUnit.KilogramForce:
                return(FromKilogramsForce(value.Value));

            case ForceUnit.Kilonewton:
                return(FromKilonewtons(value.Value));

            case ForceUnit.KiloPond:
                return(FromKiloPonds(value.Value));

            case ForceUnit.Meganewton:
                return(FromMeganewtons(value.Value));

            case ForceUnit.Newton:
                return(FromNewtons(value.Value));

            case ForceUnit.Poundal:
                return(FromPoundals(value.Value));

            case ForceUnit.PoundForce:
                return(FromPoundsForce(value.Value));

            case ForceUnit.TonneForce:
                return(FromTonnesForce(value.Value));

            default:
                throw new NotImplementedException("fromUnit: " + fromUnit);
            }
        }
Пример #23
0
 public HndzProject(Person owner, Person designer, Person consultant,
                    Person contractor, HndzCity location, /*HndzLengthUnitSystem units,*/ ICollection <HndzBuilding> buildings,
                    HndzWCS globalCoordinateSystem, HndzSite site, LengthUnit lengthUnit, AreaUnit areaUnit,
                    TemperatureUnit temperatureUnit, MassUnit massUnit, ForceUnit forceUnit) : base(HndzResources.DefaultName, HndzResources.DefaultDescription)
 {
     Owner      = owner;
     Designer   = designer;
     Consultant = consultant;
     Contractor = contractor;
     Location   = location;
     //Units = units;
     Buildings = buildings;
     GlobalCoordinateSystem = globalCoordinateSystem;
     Site            = site;
     LengthUnit      = lengthUnit;
     AreaUnit        = areaUnit;
     TemperatureUnit = temperatureUnit;
     MassUnit        = massUnit;
     ForceUnit       = forceUnit;
 }
Пример #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 Force 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 <Force>(str, formatProvider,
                                                delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                ForceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtons(x.Newtons + y.Newtons)));
        }
Пример #25
0
        public string ToString(ForceUnit 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));
        }
Пример #26
0
// ReSharper restore VirtualMemberNeverOverriden.Global

        protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ForceUnit unit)
        {
            return(unit switch
            {
                ForceUnit.Decanewton => (DecanewtonsInOneNewton, DecanewtonsTolerance),
                ForceUnit.Dyn => (DyneInOneNewton, DyneTolerance),
                ForceUnit.KilogramForce => (KilogramsForceInOneNewton, KilogramsForceTolerance),
                ForceUnit.Kilonewton => (KilonewtonsInOneNewton, KilonewtonsTolerance),
                ForceUnit.KiloPond => (KiloPondsInOneNewton, KiloPondsTolerance),
                ForceUnit.KilopoundForce => (KilopoundsForceInOneNewton, KilopoundsForceTolerance),
                ForceUnit.Meganewton => (MeganewtonsInOneNewton, MeganewtonsTolerance),
                ForceUnit.Micronewton => (MicronewtonsInOneNewton, MicronewtonsTolerance),
                ForceUnit.Millinewton => (MillinewtonsInOneNewton, MillinewtonsTolerance),
                ForceUnit.Newton => (NewtonsInOneNewton, NewtonsTolerance),
                ForceUnit.OunceForce => (OunceForceInOneNewton, OunceForceTolerance),
                ForceUnit.Poundal => (PoundalsInOneNewton, PoundalsTolerance),
                ForceUnit.PoundForce => (PoundsForceInOneNewton, PoundsForceTolerance),
                ForceUnit.ShortTonForce => (ShortTonsForceInOneNewton, ShortTonsForceTolerance),
                ForceUnit.TonneForce => (TonnesForceInOneNewton, TonnesForceTolerance),
                _ => throw new NotSupportedException()
            });
Пример #27
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 Force 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 <Force, ForceUnit>(str, formatProvider,
                                                           delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                ForceUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromNewtons(x.Newtons + y.Newtons)));
        }
Пример #28
0
        public string ToString(ForceUnit 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));
        }
Пример #29
0
        /// <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 ForceUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <ForceUnit>(str, provider, out unit));
        }
Пример #30
0
 public static bool TryParseUnit(string str, out ForceUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
 protected override ForceUnit __DoAddition(ForceUnit right)
 {
     return new Newton(Value + right.Value);
 }
 protected override ForceUnit __DoSubstraction(ForceUnit right)
 {
     return new Newton(Value - right.Value);
 }
Пример #33
0
        /// <summary>
        ///     Converts this Force to another Force with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Force with the specified unit.</returns>
        public Force ToUnit(ForceUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

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