Best way to check if string is numeric

T

tshad

I have a string field and a decimal fields and was wondering what the best
way to check for numeric is?

I have to do something like:

If (myAmount is numeric)
{
total += myAmount;
}

Thanks,

Tom
 
K

Kalpesh

Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.

Kalpesh
 
G

GS

don't know if the best but you cant ry something along the line of

private static Rage _isNumber = new Rage(@"^\d+$");

public static bool IsInteger(string theValue)
{
Match m = _isNumber.Match(theValue);
return m.Success;
} //IsInteger
 
M

Michael Nemtsev [MVP]

Hello GS,

"If u have a promblem an gonna solve it with RegExp then now u have two problems"
(c) dont remember who

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


G> don't know if the best but you cant ry something along the line of
G> private static Rage _isNumber = new Rage(@"^\d+$");
G> public static bool IsInteger(string theValue)
G> {
G> Match m = _isNumber.Match(theValue);
G> return m.Success;
G> } //IsInteger
 
J

Jon

If I remember correctly, a possible down-side with .Parse and .TryParse is that if you're trying to
read a number that is not terminated in white space (eg 25k) it will produce an error since it
regards the k in this case as invalid. This only matters of course if your number doesn't end in
white space.


Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.

Kalpesh
 
G

GS

true enough if one doesn't use regexp to begin with.
of course try parse will be better for some application
 
T

tshad

Michael Nemtsev said:
Hello GS,

"If u have a promblem an gonna solve it with RegExp then now u have two
problems"

Why is RegExp a problem?

Thanks,

tom
(c) dont remember who

---
WBR, Michael Nemtsev [.NET/C# MVP] :: blog:
http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

G> don't know if the best but you cant ry something along the line of
G> private static Rage _isNumber = new Rage(@"^\d+$");
G> public static bool IsInteger(string theValue)
G> {
G> Match m = _isNumber.Match(theValue);
G> return m.Success;
G> } //IsInteger
 
J

Jon Skeet [C# MVP]

Why is RegExp a problem?

When it's used in a "hammer to crack a nut" sense, it produces far
less maintainable code.
For genuine pattern matching, it's fine - but often there's a simpler
alternative.

It's very easy to get things wrong, regular expressions vary between
different platforms (and indeed between different versions of .NET),
you have to bear in mind escaping, etc.

Jon
 
P

Paul E Collins

GS said:
[regular expression for matching numbers]

- Doesn't handle negative numbers.
- Even if it did, might not handle foreign variations (e.g. symbol for
negative that isn't "-").
- Will match *long* strings of digits that are bigger than an integer
can store.

Int32.TryParse is a lot safer.

Eq.
 
G

Greg

I have a string field and a decimal fields and was wondering what the best
way to check for numeric is?

I have to do something like:

If (myAmount is numeric)
{
total += myAmount;

}

Thanks,

Tom

Tom,

Another approach (depending on your parsing needs) is to take a look
at an extended TextBox that deals specifically with numeric data. The
source code is small for this control.

How to: Create a Numeric Text Box
http://msdn2.microsoft.com/en-us/library/ms229644.aspx

HTH

Greg
 
K

Kalpesh

tshad,

Isn't Parse or TryParse a simple thing to do in this case?
MVPs can suggest better ways, if this doesn't help

Kalpesh
 
B

Ben Voigt [C++ MVP]

Kalpesh said:
tshad,

Isn't Parse or TryParse a simple thing to do in this case?
MVPs can suggest better ways, if this doesn't help

Parse or TryParse is in fact mandatory, because even if the string is a
numeric sequence of digits, you still can't use it for math. You *have* to
parse it into a proper numeric variable first.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
Parse or TryParse is in fact mandatory, because even if the string is a
numeric sequence of digits, you still can't use it for math. You *have* to
parse it into a proper numeric variable first.

While that's true, sometimes only validation is required. For instance,
it's reasonably common in web applications to require some client-side
validation that the user has typed in a number, but not to actually
have to *deal* with it as a number until it reaches the server.
 
B

Ben Voigt [C++ MVP]

Jon Skeet said:
While that's true, sometimes only validation is required. For instance,
it's reasonably common in web applications to require some client-side
validation that the user has typed in a number, but not to actually
have to *deal* with it as a number until it reaches the server.

The OP said

<quote>
I have to do something like:

If (myAmount is numeric)
{
total += myAmount;
}
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
The OP said

<quote>
I have to do something like:

If (myAmount is numeric)
{
total += myAmount;
}
</quote>

Ah, fair enough. Assuming "total" isn't a string as well, of course :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top