471,327 Members | 2,728 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

Determine if a text string is really a numeric type?

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
3 2282
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
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
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.

Similar topics

18 posts views Thread by Christopher W. Douglas | last post: by
20 posts views Thread by anthonymelillo | last post: by
7 posts views Thread by Doru Roman | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
4 posts views Thread by =?Utf-8?B?UmljaA==?= | last post: by
24 posts views Thread by MU | last post: by

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.