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 | | | | re: Best way to check if string is numeric
Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.
Kalpesh
On Dec 18, 11:02 am, "tshad" <t...@dslextreme.comwrote: Quote:
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
| | | | re: Best way to check if string is numeric
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" <tfs@dslextreme.comwrote in message
news:uGrJRuTQIHA.5160@TK2MSFTNGP05.phx.gbl... Quote:
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
>
>
| | | | re: Best way to check if string is numeric
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 | | | | re: Best way to check if string is numeric
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" <shahkalpesh@gmail.comwrote in message
news:f25d0a6e-f923-4485-843b-6aa0d83dabfb@i29g2000prf.googlegroups.com...
Use Decimal.Parse or TryParse.
All the numeric datatype might have Parse & TryParse.
Kalpesh | | | | re: Best way to check if string is numeric
true enough if one doesn't use regexp to begin with.
of course try parse will be better for some application
"Michael Nemtsev [MVP]" <nemtsev@msn.comwrote in message
news:3d9fba1a20e7e8ca0fbe373aa890@msnews.microsoft .com... Quote:
Hello GS,
>
"If u have a promblem an gonna solve it with RegExp then now u have two
problems" Quote:
(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
>
>
| | | | re: Best way to check if string is numeric
"Michael Nemtsev [MVP]" <nemtsev@msn.comwrote in message
news:3d9fba1a20e7e8ca0fbe373aa890@msnews.microsoft .com... Quote:
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 Quote:
(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
>
>
| | | | re: Best way to check if string is numeric
On Dec 18, 4:15 pm, "tshad" <t...@dslextreme.comwrote: Quote: Quote:
"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 | | | | re: Best way to check if string is numeric
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: Quote:
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
>
>
>
| | | | re: Best way to check if string is numeric
"GS" <gsmsnews.microsoft.comGS@msnews.Nomail.comwrote : Quote:
[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. | | | | re: Best way to check if string is numeric
On Dec 17, 10:02 pm, "tshad" <t...@dslextreme.comwrote: Quote:
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 | | | | re: Best way to check if string is numeric
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 | | | | re: Best way to check if string is numeric
"Kalpesh" <shahkalpesh@gmail.comwrote in message
news:75d2df7b-f384-4d22-a348-f5439971a017@e10g2000prf.googlegroups.com... Quote:
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. | | | | re: Best way to check if string is numeric
Ben Voigt [C++ MVP] <rbv@nospam.nospamwrote: Quote: Quote:
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 - <skeet@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 | | | | re: Best way to check if string is numeric
"Jon Skeet [C# MVP]" <skeet@pobox.comwrote in message
news:MPG.21dc902ee31f83373e@msnews.microsoft.com.. . Quote:
Ben Voigt [C++ MVP] <rbv@nospam.nospamwrote: Quote: Quote:
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> | | | | re: Best way to check if string is numeric
Ben Voigt [C++ MVP] <rbv@nospam.nospamwrote: Quote: Quote:
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 - <skeet@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 | | | | re: Best way to check if string is numeric
"Jon Skeet [C# MVP]" <skeet@pobox.comwrote in message
news:MPG.21dcd10141060544740@msnews.microsoft.com. .. Quote:
Ben Voigt [C++ MVP] <rbv@nospam.nospamwrote: Quote: Quote:
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. |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|