Int32.TryParse with multiple group delimiters

I am trying to create a method that can process and check integers, as in ok input. The problem lies in our requirements, in which the following numbers are entered as ok, regardless of the language chosen:

1500, 1.500, 1,500, 1 500 -1500, -1.500, -1,500, -1 500 1500000, 1.500.500, 1,500,500 1 500 500 -1500000, -1.500.500, -1,500,500 -1 500 500 

etc.

Now my method is as follows:

 private bool TryParseInteger(string controlValue, out int controlInt) { int number; NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands; bool result = Int32.TryParse(controlValue, styles, CultureInfo.InvariantCulture, out number); controlInt = number; return result; } 

This does not work as I want. 1.500 and 1.500.500 are not checked as the correct input.

Is there any other way to approach this?

Thanks for the help. As it turned out, 1,500.50 (etc.) should not be tested, because of which the proposed solutions do not work. Any other ideas?

+7
c #
source share
4 answers

I think the problem here is that 1.500 is understood differently for each country. For example, in the USA, equal to one and a half with some meaningless zeros, where in Germany (I, if memory serves), it is understood one thousand five hundred.

Accordingly, you should add a line at the beginning of the method as follows:

 controlValue = new string(controlValue.Where(c => !char.IsPunctuation(c) && c != ' ' || c == '-')); 

This will remove all commas, periods, and spaces that will work if you only need integers. If you would like to get a decimal number, then we would have problems ...

+3
source share

Just replace all these signs!

This code:

 string[] arr = new[] { "1500", "1.500", "1,500", "1 500", "-1500", "-1.500", "-1,500", "-1 500", "1500000", "1.500.500", "1,500,500","1 500 500", "-1500000", "-1.500.500", "-1,500,500","-1 500 500"}; foreach (var s in arr) { int i = int.Parse(s.Replace(" ", "").Replace(",", "").Replace(".", "")); Console.WriteLine(i); } 

It produces:

1500

1500

1500

1500

-1500

-1500

-1500

-1500

1,500,000

1500500

1500500

1500500

-1500000

-1500500

-1500500

-1500500

+3
source share

If you do not want to match 1,500.500 or 1.500,500 , you can replace everything . on , and try to take it apart again. Something like that:

 private bool TryParseInteger(string controlValue, out int controlInt) { int number; NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands; bool result = Int32.TryParse(controlValue, styles, CultureInfo.InvariantCulture, out number); if (!result) { controlValue = controlValue.Replace('.', ','); result = Int32.TryParse(controlValue, styles, CultureInfo.InvariantCulture, out number); } controlInt = number; return result; } 
+2
source share

You can create your own culture and change the NumberGroupSeparator as needed.

  private static bool TryParseInteger(string controlValue, out int controlInt) { String[] groupSeparators = { ",", ".", " "}; CultureInfo customCulture = CultureInfo.InvariantCulture.Clone() as CultureInfo; customCulture.NumberFormat.NumberDecimalSeparator = "SomeUnlikelyString"; NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands; bool success = false; controlInt = 0; foreach (var separator in groupSeparators) { customCulture.NumberFormat.NumberGroupSeparator = separator; success = Int32.TryParse(controlValue, styles, customCulture, out controlInt); if (success) { break; } } return success; } 
0
source share

All Articles