• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1432

# convert comma delimited string from textbox to decimal??

I have a textbox where users will enter in an unknown amount comma delimited numbers and I then need to process that textbox string as numbers.

How do I convert that string to decimal numbers?  Should I use an arraylist or what to handled the numbers?

TIA
0
wheels4me
3 Solutions

Middle School Assistant TeacherCommented:
Use something like...

private void button1_Click(object sender, EventArgs e)
{
decimal dec;
string[] numbers = textBox1.Text.Split(",".ToCharArray());
foreach (string number in numbers)
{
try
{
dec = Decimal.Parse(number);
// do something with "dec"
System.Diagnostics.Debug.WriteLine(dec.ToString());
}
catch (Exception ex)
{
MessageBox.Show(number, ex.Message, MessageBoxButtons.OK );
}
}
}
0

Commented:
sure, if you want to just populate them into an array  you could do something like this:

String str = "15.45, 43.5";  //str = myTextBox.Text;
String[] arr = str.Split(',');
ArrayList arrTemp = new ArrayList();
for(int i=0; i<arr.Length; i++)
{
}
0

Commented:
You may want to consider a different interface, since many people will enter:

1,350.56 as a single number

Not as two different numbers 1 and 350.56
0

Author Commented:
Thanks for the help.  I split the points since all the answers had value to them.  I figured out what I was trying to do before I got the answer.  What I used was close to the second answer -- String[] arr = str.Split(',').

It was a homework assignment for school and at first I thought it read that we had to use an ArrayList, further investigation found that I could create an array and not assign it a length until run-time/data entry.

Thanks for the help.  It has been a couple of years since I used EE and I am glad to see that it works as well as ever.

Thanks again,

Kevin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.