Hi!
How can I insert commas in numbers.
For example, number is 123456789 , now I want that after inserting commas it should become 12,34,56,789....
how can i do this.......

Recommended Answers

All 12 Replies

Well you could try converting your int into a string then format it the way you want...Anyway you just cant insert commas into a int type directly.

I tried , but its giving answer according to international system....
And i want it according to indian system of numeration....
the code which i am using is:

private void button2_Click(object sender, EventArgs e)
        {
            string bb = this.textBox1.Text.Trim();
            int num1 = Convert.ToInt32(bb);
            res2 = String.Format("{0:##,##}", num1);
            this.textBox3.Text = res2;
        }

How to modify my code

Try this :

int i = 123456789;
            //string s = i.ToString("##,##,##,###"); -->will not work
            string s = i.ToString("##-##-##-###"); // --> will work
            s = s.Replace('-', ',');

Try this :

int i = 123456789;
            //string s = i.ToString("##,##,##,###"); -->will not work
            string s = i.ToString("##-##-##-###"); // --> will work
            s = s.Replace('-', ',');

ur code works but if i am entering the number 1234567, it shows comma before the number .........
tell me general method for placing commas so that it is applicable for max 15 digit numbers.

Use string Insert method:

int i = 1234567;
            string s = i.ToString();
            for(int p=2; p < s.Length; p+=3)
            {
                s = s.Insert(p, ",");
            }

Use string Insert method:

int i = 1234567;
            string s = i.ToString();
            for(int p=2; p < s.Length; p+=3)
            {
                s = s.Insert(p, ",");
            }

u r giving me the code for specific numbers....
kindly tell me general logic , such that if user enter any number he should get correct answer.

My last snippet works for integers from 1 to MaxInt. Try it!
This snippet just tells you how it could be done. How it should be done is entirly up to you to decide.
Another possibility is the use of the SubString method and to concatenate all the substrings together with ",".

thanks a lot ........
I have done it

If this solves your problem, coud you mark this thread as solved?
You don't have to do this for my beautifull eyes, do it for the other members of this community :)

myNumber.ToString("#,###");

This is a good thread. I am going to try doing it without .ToString and .Insert
My instructor wants me to do it this way. When I figure it out I will post it to this thread.

Don't post to a thread that was ended 2 years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.