Solved

convert comma delimited string from textbox to decimal??

Posted on 2006-06-09
5
1,384 Views
Last Modified: 2008-01-09
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
Comment
Question by:wheels4me
5 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
Comment Utility
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
 
LVL 7

Accepted Solution

by:
prosh0t earned 125 total points
Comment Utility
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++)
{
      arrTemp.Add(Convert.ToDecimal(arr[i].Trim()));
}
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 50 total points
Comment Utility
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 Comment

by:wheels4me
Comment Utility
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now