469,621 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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

"tshad" <tf*@dslextreme.comwrote in message
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
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.
"Kalpesh" <sh*********@gmail.comwrote in message
news:f2**********************************@i29g2000 prf.googlegroups.com...
Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.

Kalpesh
Dec 18 '07 #5
GS
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
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"tshad" wrote:
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
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
Dec 19 '07 #12

"Kalpesh" <sh*********@gmail.comwrote in message
news:75**********************************@e10g2000 prf.googlegroups.com...
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.
>
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.

Similar topics

5 posts views Thread by Daniel Pryde | last post: by
33 posts views Thread by Steven Bethard | last post: by
136 posts views Thread by Matt Kruse | last post: by
4 posts views Thread by John Salerno | last post: by
6 posts views Thread by GarryJones | last post: by
29 posts views Thread by calvert4rent | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.