How to check if a string is a (very large) number?

I need to do a number check for the specified string. The problem is that a string can be a large number , more than any numeric type in C # can represent, so I cannot use TryParse functions because they will only give information about whether they can convert to these types.

It should take into account the - / +, delimiter and current culture settings.

I found some solutions using regular expressions, but they all miss some details. And I'm not good at regular expressions

+4
source share
3 answers

You can use BigInteger if you are using C # 4.0 and numbers are integers.

This Represents an arbitrarily large signed integer.

Use the TryParse method to avoid a possible exception from Parse (unless you are sure that the passed string will always be a real integer).

+9
source

If you are using .NET 4.0, you can use BigInteger . It contains TryParse , which accepts IFormatProver , which will do what you need. BigInteger has unlimited size, so it is impossible for the numbers you need to be larger than BigInteger can handle.

+3
source

Take a look at http://www.regexlib.com/Search.aspx?k=numeric&c=-1&m=5&ps=20 . http://regexlib.com/ contains many examples of regular expressions, and there should be one that matches your requirements.

0
source

All Articles