473,809 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine if a string represents a valid numerical value?

Folks,

I need to try and make sure that a given string is a valid numerical
value (not just int, or int32 - it could be float, decimal, what have
you). VB.NET has a neat "IsNumeric" function - too bad C# doesn't have
anything similar. Or does it??

I was thinking about trying to convert the string to Int32, but as I
mentioned - that alone won't be good enough, and I really don't want
to attempt a dozen conversions to find out - nor do I really want to
start parsing the string for numerical characters (since there can be
"+", "-", "." and many more extra character and it's still a valid
numeric).

Any ideas? Thoughts?

Thanks!
Marc

=============== =============== =============== =============== ====
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)i nova.ch
Nov 15 '05 #1
5 6623
Two ways. You can either try to convert the string in a try block. If an
exception is thrown, it is not a number.

Alternatively, you can reference the VB.NET assembly (which is located in
the references dialog) and use the IsNumeric function available to VB
developers.

"Marc Scheuner [MVP ADSI]" <m.********@ino va.SPAMBEGONE.c h> wrote in message
news:s7******** *************** *********@4ax.c om...
Folks,

I need to try and make sure that a given string is a valid numerical
value (not just int, or int32 - it could be float, decimal, what have
you). VB.NET has a neat "IsNumeric" function - too bad C# doesn't have
anything similar. Or does it??

I was thinking about trying to convert the string to Int32, but as I
mentioned - that alone won't be good enough, and I really don't want
to attempt a dozen conversions to find out - nor do I really want to
start parsing the string for numerical characters (since there can be
"+", "-", "." and many more extra character and it's still a valid
numeric).

Any ideas? Thoughts?

Thanks!
Marc

=============== =============== =============== =============== ====
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)i nova.ch

Nov 15 '05 #2
> I was thinking about trying to convert the string to Int32, but as I
mentioned - that alone won't be good enough, and I really don't want


Here are a few ways:

1. Pick the lowest common denominator. Cast it to a decimal.
2. Use a regex. There are plenty of places you can find the right pattern
if you dont know it.
3. Use a control that does it. In some cases in my windows.forms app I am
using the numeric up down control.
4. Catch the KeyDown event and check the input.
etc..
etc...

JF
Nov 15 '05 #3
Hello Marc,
I need to try and make sure that a given string is a
valid numerical value (not just int, or int32 - it
could be float, decimal, what have you). VB.NET has
a neat "IsNumeric" function - too bad C# doesn't have
anything similar. Or does it??


The static Double.TryParse method may be of interest to you.

Hope this helps,
Len
Nov 15 '05 #4
I think the easiest way would be to use a simple regular expression. I
have been using regular expressions and I have found them to be very
easy and extremely powerful and flexible. A great source for learing
them is Mastering Regular Expressions - and O Reily book or get it from
www.regexlib.com.

Matt

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
Out of curiosity, what do you want back as the answer? A boolean? An
exception?

--
..tom
remove email address' dashes for replies
opensource middleware at <http://isectd.sourcefo rge.net>
http://gagne.homedns.org
Nov 15 '05 #6

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

Similar topics

6
2163
by: H | last post by:
This is a question that has haunted me for quite some time. if you build a 4 tier database application where the 4th tier is the database server (MS SQL 2000), where do you build the connection string? The obvious answer is to hard code it into the middle layer and use a config file. The problem is the database application needs to be distributed to purchasing clients. Therefore, you do not know, at development, what the SQL
13
2496
by: Gil | last post by:
I have a string : string A = "2"; I need to get it's integer value : // ?? int val = A ; // ??
108
6477
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
9
7136
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
16
3431
by: Jm | last post by:
Hi All Is it possible to determine who is logged onto a machine from inside a service using code in vb.net ? I have found some code that seems to work under vb6, but doesnt under .NET ? Any help is greatly appreciated Thanks
13
3229
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
8
2258
by: farah727rash | last post by:
Hi all, I am trying to find the numerical value of a string that stores a two digit number. I have found the numerical value of a char as: char character; cin >character; int number = character - 48; // Computing the numberical value of character entered cout << "The number you entered is: " << number << endl << endl;
29
5121
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two questions. Maybe someone can help? using System; using System.Collections.Generic; using System.Text;
27
6083
by: artifact.one | last post by:
Hello. I want to be able to portably determine whether or not a given double value is negative. Under C99, I do: if (signbit(d)) negative = 1; Or, if the implementation doesn't provide signbit(), I do:
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.