Ejemplo n.º 1
0
        internal static bool TryParse(string Value, ref double Result)
        {
            CultureInfo      cultureInfo            = Utils.GetCultureInfo();
            NumberFormatInfo numberFormat           = cultureInfo.NumberFormat;
            NumberFormatInfo normalizedNumberFormat = DecimalType.GetNormalizedNumberFormat(numberFormat);

            Value = Utils.ToHalfwidthNumbers(Value, cultureInfo);
            if (numberFormat == normalizedNumberFormat)
            {
                return(double.TryParse(Value, NumberStyles.Any, (IFormatProvider)normalizedNumberFormat, out Result));
            }
            try
            {
                Result = double.Parse(Value, NumberStyles.Any, (IFormatProvider)normalizedNumberFormat);
                return(true);
            }
            catch (FormatException ex1)
            {
                try
                {
                    return(double.TryParse(Value, NumberStyles.Any, (IFormatProvider)numberFormat, out Result));
                }
                catch (ArgumentException ex2)
                {
                    return(false);
                }
            }
            catch (StackOverflowException ex)
            {
                throw ex;
            }
            catch (OutOfMemoryException ex)
            {
                throw ex;
            }
            catch (ThreadAbortException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>Returns a <see langword="Double" /> value that corresponds to the specified string and number format information. </summary>
        /// <param name="Value">Required. String to convert to a <see langword="Double" /> value.</param>
        /// <param name="NumberFormat">A <see cref="T:System.Globalization.NumberFormatInfo" /> object that defines how numeric values are formatted and displayed, depending on the culture.</param>
        /// <returns>The <see langword="Double" /> value corresponding to <paramref name="Value" />.</returns>
        public static double Parse(string Value, NumberFormatInfo NumberFormat)
        {
            CultureInfo cultureInfo = Utils.GetCultureInfo();

            if (NumberFormat == null)
            {
                NumberFormat = cultureInfo.NumberFormat;
            }
            NumberFormatInfo normalizedNumberFormat = DecimalType.GetNormalizedNumberFormat(NumberFormat);

            Value = Utils.ToHalfwidthNumbers(Value, cultureInfo);
            try
            {
                return(double.Parse(Value, NumberStyles.Any, (IFormatProvider)normalizedNumberFormat));
            }
            catch (FormatException ex) when(NumberFormat != normalizedNumberFormat)
            {
                return(double.Parse(Value, NumberStyles.Any, (IFormatProvider)NumberFormat));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }