473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 12171
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Pryde | last post by:
Hi everyone. I was wondering if anyone might be able to help me out here. I'm currently looking to find the quickest way to find a best fit match in a large array. My problem is that I have an...
33
by: Steven Bethard | last post by:
Is there a good way to determine if an object is a numeric type? Generally, I avoid type-checks in favor of try/except blocks, but I'm not sure what to do in this case: def f(i): ... if x < i:...
5
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: MLH | last post by:
I want to change my system time date each time an A97 app is started. Here's how I've been doing it. Am looking for a better way. Sure some of you have researched this. Function...
4
by: John Salerno | last post by:
My code is below. The main focus would be on the OnStart method. I want to make sure that a positive integer is entered in the input box. At first I tried an if/else clause, then switched to...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
6
by: GarryJones | last post by:
The following function checks to see if a variable read from a mysql database is numeric. The funtion worked until I hit the value 15 303 That is a valid number but because of the space between...
29
by: calvert4rent | last post by:
I need to some sort of data type that will hold a listing of ID's and their counts/frequency. As I do some processing I get an ID back which I need to store and keep an accurate count for how many...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.