By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,729 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,729 IT Pros & Developers. It's quick & easy.

Convert unknown string contents to numeric value

P: n/a
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

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1837 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
Jan 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
look at Double.TryParse
Jan 23 '07 #2

P: n/a
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.
On Tue, 23 Jan 2007 07:28:56 +0100, Mark Chimes <ma********@gmail.com>
wrote:
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

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1837 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!



--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 23 '07 #3

P: n/a
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 Chimes" <ma********@gmail.comwrote in message
news:45*********************@uq-127creek-reader-03.brisbane.pipenetworks.com.au...
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

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1837 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1837 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
Jan 23 '07 #4

P: n/a
Mark Chimes wrote:
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

Jan 23 '07 #5

P: n/a
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

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
Mark Chimes wrote:
>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
--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 1840 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
Jan 25 '07 #6

P: n/a
Mark Chimes <ma********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.