PC Review


Reply
Thread Tools Rate Thread

Convert unknown string contents to numeric value

 
 
Mark Chimes
Guest
Posts: n/a
 
      23rd Jan 2007
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!


 
Reply With Quote
 
 
 
 
=?Utf-8?B?WE9S?=
Guest
Posts: n/a
 
      23rd Jan 2007
look at Double.TryParse
 
Reply With Quote
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      23rd Jan 2007
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 <(E-Mail Removed)>
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]
 
Reply With Quote
 
Mark Chimes
Guest
Posts: n/a
 
      23rd Jan 2007
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" <(E-Mail Removed)> wrote in message
news:45b5ab28$0$490$(E-Mail Removed)...
> 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!


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jan 2007
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

 
Reply With Quote
 
Mark Chimes
Guest
Posts: n/a
 
      25th Jan 2007
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jan 2007
Mark Chimes <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string value to numeric value alhotch Microsoft Access Queries 3 26th May 2010 01:47 PM
Convert string to Numeric in VB.net 2003 ? engteng Microsoft VB .NET 9 30th Sep 2008 10:24 PM
Convert String to Numeric Values JenISCM Microsoft Access 2 9th Jan 2008 06:41 PM
How to convert contents of column from numeric data type to text moondaddy Microsoft Excel Discussion 6 29th Apr 2006 11:54 PM
How do I convert a string to a numeric value in Access? =?Utf-8?B?S2lzaG1hdHVzaA==?= Microsoft Access Queries 1 4th Aug 2005 05:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:22 AM.