Exemple #1
0
 public void CheckNumberTest()
 {
     Assert.IsFalse(StringConversion.CheckNumber(new string[] { }, ".", "", false, false).FoundValueFormat != null);
     Assert.IsTrue(StringConversion.CheckNumber(new[] { "16673" }, ".", "", false, false).FoundValueFormat != null);
     Assert.AreEqual(DataType.Integer,
                     StringConversion.CheckNumber(new[] { "16673" }, ".", "", false, false).FoundValueFormat.DataType);
     Assert.IsFalse(
         StringConversion.CheckNumber(new[] { "16673", "A Test" }, ".", "", false, false).FoundValueFormat != null);
     Assert.AreEqual(DataType.Numeric,
                     StringConversion.CheckNumber(new[] { "16673", "-23", "1.4" }, ".", "", false, false).FoundValueFormat.DataType);
 }
Exemple #2
0
        public static CheckResult GuessNumeric(IList <string> samples, bool guessPercentage,
                                               bool allowStartingZero, CancellationToken cancellationToken)
        {
            var checkResult = new CheckResult();

            var possibleGrouping = new List <char>();

            // Determine which decimalGrouping could be used
            foreach (var caracter in StringConversion.DecimalGroupings)
            {
                if (caracter == '\0')
                {
                    continue;
                }
                foreach (var smp in samples)
                {
                    if (smp.IndexOf(caracter) <= -1)
                    {
                        continue;
                    }
                    possibleGrouping.Add(caracter);
                    break;
                }
            }

            possibleGrouping.Add('\0');
            var possibleDecimal = new List <char>();

            foreach (var caracter in StringConversion.DecimalSeparators)
            {
                if (caracter == '\0')
                {
                    continue;
                }
                foreach (var smp in samples)
                {
                    if (smp.IndexOf(caracter) <= -1)
                    {
                        continue;
                    }
                    possibleDecimal.Add(caracter);
                    break;
                }
            }

            // Need to have at least one decimal separator
            if (possibleDecimal.Count == 0)
            {
                possibleDecimal.Add('.');
            }

            foreach (var thousandSeparator in possibleGrouping)
            {
                // Try Numbers: Int and Decimal
                foreach (var decimalSeparator in possibleDecimal)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    if (decimalSeparator.Equals(thousandSeparator))
                    {
                        continue;
                    }
                    var res = StringConversion.CheckNumber(samples, decimalSeparator, thousandSeparator, guessPercentage,
                                                           allowStartingZero);
                    if (res.FoundValueFormat != null)
                    {
                        return(res);
                    }

                    checkResult.KeepBestPossibleMatch(res);
                }
            }

            return(checkResult);
        }