469,621 Members | 1,685 Online

# Best way to check if string is numeric

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
Dec 18 '07 #1
16 11862
Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.

Kalpesh

On Dec 18, 11:02 am, "tshad" <t...@dslextreme.comwrote:
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
Dec 18 '07 #2
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

news:uG**************@TK2MSFTNGP05.phx.gbl...
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

Dec 18 '07 #3
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
Gdon't know if the best but you cant ry something along the line of
Gprivate static Rage _isNumber = new Rage(@"^\d+\$");
Gpublic static bool IsInteger(string theValue)
G{
GMatch m = _isNumber.Match(theValue);
Greturn m.Success;
G} //IsInteger
Dec 18 '07 #4
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.
"Kalpesh" <sh*********@gmail.comwrote in message
Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.

Kalpesh
Dec 18 '07 #5
true enough if one doesn't use regexp to begin with.
of course try parse will be better for some application

"Michael Nemtsev [MVP]" <ne*****@msn.comwrote in message
news:3d**************************@msnews.microsoft .com...
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
Gdon't know if the best but you cant ry something along the line of
Gprivate static Rage _isNumber = new Rage(@"^\d+\$");
Gpublic static bool IsInteger(string theValue)
G{
GMatch m = _isNumber.Match(theValue);
Greturn m.Success;
G} //IsInteger

Dec 18 '07 #6
"Michael Nemtsev [MVP]" <ne*****@msn.comwrote in message
news:3d**************************@msnews.microsoft .com...
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

Gdon't know if the best but you cant ry something along the line of
Gprivate static Rage _isNumber = new Rage(@"^\d+\$");
Gpublic static bool IsInteger(string theValue)
G{
GMatch m = _isNumber.Match(theValue);
Greturn m.Success;
G} //IsInteger

Dec 18 '07 #7
On Dec 18, 4:15 pm, "tshad" <t...@dslextreme.comwrote:
"If u have a promblem an gonna solve it with RegExp then now u have two
problems"

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
Dec 18 '07 #8
in the Microsoft.VisualBasic namespace, there is a very complete
implementation of IsNumeric. Of course, you may not need or even want such a
"feature rich" implementation, but you could take a look.
-- Peter
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
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
Dec 18 '07 #9
"GS" <gs**********************@msnews.Nomail.comwrote :
[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.
Dec 18 '07 #10
On Dec 17, 10:02 pm, "tshad" <t...@dslextreme.comwrote:
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
Dec 19 '07 #11

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

Kalpesh
Dec 19 '07 #12

"Kalpesh" <sh*********@gmail.comwrote in message

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.
>
Kalpesh

Dec 26 '07 #13
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 26 '07 #14

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
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.

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;
}
</quote>
>
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Dec 26 '07 #15
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
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;
}
</quote>
Ah, fair enough. Assuming "total" isn't a string as well, of course :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 26 '07 #16

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
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;
}
</quote>

Ah, fair enough. Assuming "total" isn't a string as well, of course :)
Then it would compile but it would still be wrong.
>
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Dec 27 '07 #17

### This discussion thread is closed

Replies have been disabled for this discussion.