473,396 Members | 2,021 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,396 software developers and data experts.

Detect if a string is s number

Hi,

In C#, I have vaiables declared as string, how can I detect if they are
numeric? Like VB has IsNumeric function.

TIA
May 28 '06 #1
4 2033
"Danny Ni" <dn*@yahoo.com> wrote:
In C#, I have vaiables declared as string, how can I detect if they are
numeric? Like VB has IsNumeric function.


Try to parse it with int.TryParse() or double.TryParse(). You can
discard the result. For example:

---8<---
public static bool IsNumeric(string value)
{
double unused;
return double.TryParse(value, out unused);
}
--->8---

Mind internationalization, if you need to always parse '.' as decimal
versus ',' etc.

-- Barry

--
http://barrkel.blogspot.com/
May 28 '06 #2
Hello Danny,

Besides parsing, as Barry recomended, there are several other way

- Char.IsDigit for each char in string
- Use Regexp to check string

And look for this discussion there http://groups.google.com/group/micro...a0af96fe705eba

DN> Hi,
DN>
DN> In C#, I have vaiables declared as string, how can I detect if they
DN> are numeric? Like VB has IsNumeric function.
DN>
DN> TIA
DN>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 28 '06 #3
Regex seems to be the most effecient and shortest way though; beside
the syntax would look very short.
For the suggestion of Char.IsDigit there might be an issue with
decimals, large exp, ..etc. to consider

May 29 '06 #4
Hello coosa,

c> Regex seems to be the most effecient and shortest way though; beside
c> the syntax would look very short.
c> For the suggestion of Char.IsDigit there might be an issue with
c> decimals, large exp, ..etc. to consider

There is one possible way, add reference to the Microsoft.VisualBasic and
use IsNumeric ;)

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Fabian | last post by:
function getImage(id,x,y) { if (mpdef == undefined) { // set to default image document.getElementById(id).src = terrdef; } else { document.getElementById(id).src = terrdef ]; } return; }
23
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I...
1
by: oreng | last post by:
Hey all, I have some problems detecting whether the client's browser javascript is enabled at the server side. While Request.Browser.JavaScript only check if the browser enable java script (and...
4
by: Jim Michaels | last post by:
how do I write an overloaded >operator for istream that let's say fraction class through set() can take several types: void set(char*) void set(long int num, long int den) void set(double) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.