473,668 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert unknown string contents to numeric value

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
6 3961
look at Double.TryParse
Jan 23 '07 #2
Hi Mark,

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

Decimal.Parse(s tring) // throws an exception if it is not a valid decimal
Convert.ToDecim al(string) // throws an exception if it is not a valid
decimal

Decimal d;
Decimal.TryPars e(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********@gma il.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
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.reg ularExpressions ;
....
bool boolIsAlpha = Regex.IsMatch(s trData, @"^\p{L}*$") ;
cheers,
Mark Chimes

"Mark Chimes" <ma********@gma il.comwrote in message
news:45******** *************@u q-127creek-reader-03.brisbane.pip enetworks.com.a u...
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
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.reg ularExpressions ;
...
bool boolIsAlpha = Regex.IsMatch(s trData, @"^\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
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.co mwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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.reg ularExpressions ;
...
bool boolIsAlpha = Regex.IsMatch(s trData, @"^\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
Mark Chimes <ma********@gma il.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.TryPars e, or Morten suggested.

--
Jon Skeet - <sk***@pobox.co m>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2821
by: Ivan Debono | last post by:
Hi, I keep getting an Unknown runtime error on line 3 below: 1 If oField.Type <> 136 Then 'adChapter 2 If Right(oField.name, 3) = "_id" Then 3 For Each oKey In oTable.Keys 4 If oKey.Type = 2 Then 'adKeyForeign 5 Set oCol = oKey.Columns(0)
4
9746
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
4
17675
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
37
11055
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there anyone that could tell me how to convert these two things? 1)
14
1463
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the integer that was entered. So, if 353 was entered, the output should be 11.
4
25952
by: simonZ | last post by:
Why this don't work: Boolean test; String testValue; testValue="0"; test=System.Convert.ToBoolean(testValue); How can I convert string to boolean?
10
26609
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
14
2920
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also contain zero
9
8236
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.