473,396 Members | 2,139 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.

Q: convert to numbers

Hello,

string newStr=myStr.Substring(1,3);
int newNum = System.Convert.ToInt32(newStr);

This fails if newStr does not have all characters as numbers. How can you do
this in a professional way?

Thanks,
Jim.

Nov 19 '05 #1
4 1367
int newNum;
try
{
newNum=Convert.ToInt32(newStr);
}
cacth
{
newNum=0; //Or other default value
}

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
Hello,

string newStr=myStr.Substring(1,3);
int newNum = System.Convert.ToInt32(newStr);

This fails if newStr does not have all characters as numbers. How can you do this in a professional way?

Thanks,
Jim.

Nov 19 '05 #2
You can check if it is a number by using Int32.Parse() and a try/catch. If
it raises an exception, you handle it and it isn't a number.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
Hello,

string newStr=myStr.Substring(1,3);
int newNum = System.Convert.ToInt32(newStr);

This fails if newStr does not have all characters as numbers. How can you
do
this in a professional way?

Thanks,
Jim.

Nov 19 '05 #3
You'll have to place the Convert.ToInt32 call inside of a try / catch
block. In 2.0 there will be TryParse methods that don't throw.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 3 Jun 2005 12:05:16 -0700, JIM.H.
<JI**@discussions.microsoft.com> wrote:
Hello,

string newStr=myStr.Substring(1,3);
int newNum = System.Convert.ToInt32(newStr);

This fails if newStr does not have all characters as numbers. How can you do
this in a professional way?

Thanks,
Jim.


Nov 19 '05 #4

.... or write your own IsNumeric method ...

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 3 Jun 2005 12:05:16 -0700, JIM.H.
<JI**@discussions.microsoft.com> wrote:
Hello,

string newStr=myStr.Substring(1,3);
int newNum = System.Convert.ToInt32(newStr);

This fails if newStr does not have all characters as numbers. How can you do
this in a professional way?

Thanks,
Jim.


Nov 19 '05 #5

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

Similar topics

13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: ^_^ | last post by:
How to convert HEX <-> DEC constant value in C source file, and write out the result c files? a=4; b=6; from or to a=0x4;
13
by: Jason | last post by:
Could someone here show me how I would write a vb program to convert decimal ip address to binary? For example a small form with a convert button and a label for the result and a textbox for the...
28
by: MLH | last post by:
The largest integer A97 can deal with is 2,147,483,647, as I understand it from HELP. I would be content to represent larger integers as strings. For example, "2147483648" would suit me fine. I...
5
by: Bob Homes | last post by:
In VB6, foreground and background colors of controls had to be assigned a single number. If you knew the RGB values for the color, you still had to convert them into the single number accepatable...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
8
by: =?Utf-8?B?UmljYXJkbyBRdWludGFuaWxsYQ==?= | last post by:
i need to convert data from string to nibble wich (nibble is a four bits representation) As example i have the following code string data1 = "12345678"; so ¿how can i convert this data...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
8
code green
by: code green | last post by:
I am being sent a CSV file with ID numbers somehow converted to scientific format ie 1.93E+11 and 3.70E+9. In theory the same thing could happen to telephone numbers or even currency. I am trying...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.