Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert comma delimited string from textbox to decimal??

Posted on 2006-06-09
5
Medium Priority
?
1,430 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
4 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 16872457
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 500 total points
ID: 16872476
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 200 total points
ID: 16872490
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
ID: 16874467
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

879 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