The correct way to check if a value is numeric in C #

I just need to know if the value is numeric. I do not need to do anything with value. Is this the best way? Feel dirty by creating a variable that I will never use outside of this:

int val; if(int.TryParse(txtFoo.Text, out val)) { .... } 
+6
source share
8 answers

Yes, using the appropriate TryParse method and ignoring the out parameter is the best way to do this.

You can wrap this in your own set of helper methods (which can indicate the appropriate culture, etc., if not suitable for you by default) and just return the bool without the out parameter to make it easier to call them.

Of course, you need to figure out which type of parsing is most appropriate - even for integers, you need to consider whether the Int32 range is enough for your use case. In my experience, most numerical inputs have their own β€œnatural” range of permissible values, which is unlikely to exactly match the range of any predefined type. Therefore, you can extend your helper methods to include a range of valid values ​​for adoption.

+3
source

It is not as flexible as int.TryParse , but you can check if each character is a number:

 bool isInt = txtFoo.Text.All(c => char.IsNumber(c)); 

In general, I would recommend sticking to int.TryParse . You can even call the unused parameter "ignored" to be explicit about your intent, for example:

 int ignored; bool isInt = int.TryParse(txtFoo.Text, out ignored); 
+2
source

This is the best way to do this in my knowledge - that the standards of our company are adhered to in any case due to error handling performed in parsing.

This provides the following benefits: http://www.dotnetperls.com/int-tryparse

+1
source

This is the recommended way to do this in C #. However, you can add Microsoft.VisualBasic.dll as a reference to your project, and then use Microsoft.VisualBasic.Information.IsNumeric()

+1
source

You can use regular expressions

 Regex _isNumber = new Regex(@"^\d+$"); _isNumber.IsMatch(txtFoo.Text); 

This will only match Ints, but you can write one that also matches decimals.

+1
source

"is numeric" is an ambiguous term.

  • Culture in the know?
  • Allow thousands and / or decimal separators?
  • Allow scientific notation?
  • Allow sign (before? After? ...)
  • What range of values ​​do you allow? Signed 32-bit integer (Int32.TryParse), Unsigned 32-bit integer (UInt32.TryParse), decimal, double, ...

Therefore, there is no β€œbetter” way, and the Framework provides many different ways to analyze numbers.

+1
source

You can try using Regex parsing to determine that there are no non-numeric characters in the string, or you can use Int.TryParse (), Double.TryParse (), Float.TryParse () depending on the input.

0
source
 bool test (string teststring) { for (i=0;i==teststring.length;i++){ if instr("0123456789.,-+Ee",teststring.substring(i,1) <0){return false;} // some additional tests below here if you like return true; } 

however, E1001E12e.12e will be marked as a number, it will take a little more magic to perform a clean check, but then you can determine if it has an int or float.

0
source

Source: https://habr.com/ru/post/885438/


All Articles