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

Determine if a text string is really a numeric type?

P: n/a
Hi all,

What function lets me check a text string and determine if that string is really a int, float, double, etc. just formatted as a string?

Thanks in advance!

Adam
Jan 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You can use the TryParse method.
http://msdn.microsoft.com/library/de...parsetopic.asp

--
Tim Wilson
..NET Compact Framework MVP

"AdamM" <no****@nospam.com> wrote in message
news:Br********************@comcast.com...
Hi all,

What function lets me check a text string and determine if that string is
really a int, float, double, etc. just formatted as a string?

Thanks in advance!

Adam
Jan 21 '06 #2

P: n/a
If you are using VS2003 then the Double class has a TryParse method,
but for other numeric types you have to perform a Parse and catch any
resulting FormatException.

In VS2005 all of the numeric types have TryParse methods.

However, there is no method that will take a string and guess what type
of number was formatted into it. It would be difficult to write one.
For example, was the string "24.345" formatted from a double, a float,
or a decimal? It's impossible to say. Similarly, was "524" a short, an
int, a long, a decimal, a double, or a float? Surely it wasn't a
byte... the number's too large... but you can see the problems involved
in figuring out the exact type of the original value.

Certainly you can figure out if the string contains a valid number...
that's what TryParse (or, in VS2003, Parse) does for you.

Jan 21 '06 #3

P: n/a
Certainly you can figure out if the string contains a valid number...
that's what TryParse (or, in VS2003, Parse) does for you.


It's even harder than that -
What is 23,40 ?

1) A string containing 2 integers
2) A float for continental europeans

Also check whether TryParse checks the whole string - it probably only looks
for a number at the start and ignores everything after.

There is no way of knowing without sufficient context
Jan 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.