Convert unknown string contents to numeric value

  • Thread starter Thread starter Mark Chimes
  • Start date Start date
M

Mark Chimes

Hi All,

I need to search thru some strings and discard them if they canot be
converted to a decimal or interger value. What is the best way to do this?

cheers,
Mark Chimes
 
Hi Mark,

You have three ways of doing this. Since all integers are also valid
decimal numbers use

Decimal.Parse(string) // throws an exception if it is not a valid decimal
Convert.ToDecimal(string) // throws an exception if it is not a valid
decimal

Decimal d;
Decimal.TryParse(string, out d) // returns false if it is not a valid
decimal, d will contain the number

Anyhow, instead of using Decimal, you may be thinking of a Double, if so,
substitute Decimal with Double in the above code.
 
Hi Guys,

Thanks for the replies.
For the sake of others who may need this information and browse thru this
newsgroup, here is the "answer" I used to resolve this issue.

Iused the following code, using regular expressions, to check if the string
contained non-numeric characters. If so, the string is rejected.

using System.Text.regularExpressions;
....
bool boolIsAlpha = Regex.IsMatch(strData, @"^\p{L}*$");


cheers,
Mark Chimes
 
Mark said:
Thanks for the replies.
For the sake of others who may need this information and browse thru this
newsgroup, here is the "answer" I used to resolve this issue.

Iused the following code, using regular expressions, to check if the string
contained non-numeric characters. If so, the string is rejected.

using System.Text.regularExpressions;
...
bool boolIsAlpha = Regex.IsMatch(strData, @"^\p{L}*$");

IMO, that's not a good solution:

1) It just doesn't work. It assumes that you can have anything other
than letters, and that you don't need any numbers. Here are some
strings which shouldn't be accepted but are:
""
"1+1"
"-1-1"
"1.0.0"
"1$2"

2) Using regular expressions here rather than normal code is harder to
read, IMO
3) Using regular expressions here is slower than a hard-coded
"pre-check" followed by
a simple call to TryParse. (Calling TryParse may well be fast enough;
regular expressions will be a lot slower)
4) It does no bounds checking - so even though the digits 1-10 a
thousand times each *is* a number, it's out of range of any .NET type.

Jon
 
Jon,

Hmmm. The data I need to parse will never hold any of the sort of values you
list, but I see what you mean.

Here's my problem.
I have large amounts of data coming to my form in an unknown state. ie: I
have no idea whether it is numerical, alpha-numerical or all alpha. For
example, I may receive "5.00%" (discard), "Total:" (discard), 2748.512
(use).

I am interested ONLY in pure numerical data, including decimal values.
I need to use only approx 5% of the data that comes to the form.

Any suggestions?

cheers,
Mark
 
Mark Chimes said:
Hmmm. The data I need to parse will never hold any of the sort of values you
list, but I see what you mean.

Here's my problem.
I have large amounts of data coming to my form in an unknown state. ie: I
have no idea whether it is numerical, alpha-numerical or all alpha. For
example, I may receive "5.00%" (discard), "Total:" (discard), 2748.512
(use).

I am interested ONLY in pure numerical data, including decimal values.
I need to use only approx 5% of the data that comes to the form.

Any suggestions?

Yes - use Decimal.TryParse, or Morten suggested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top