1,105,380 Community Members

insert commas between digits in number

Member Avatar
vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

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.......

Member Avatar
colon3l
Newbie Poster
9 posts since Aug 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

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

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

Try this :

int i = 123456789;
            //string s = i.ToString("##,##,##,###"); -->will not work
            string s = i.ToString("##-##-##-###"); // --> will work
            s = s.Replace('-', ',');
Member Avatar
vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

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.

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

Use string Insert method:

int i = 1234567;
            string s = i.ToString();
            for(int p=2; p < s.Length; p+=3)
            {
                s = s.Insert(p, ",");
            }
Member Avatar
vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

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.

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

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 ",".

Member Avatar
vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

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

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

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 :)

Question Answered as of 4 Years Ago by ddanbe and colon3l
Member Avatar
xcalibur37
Newbie Poster
1 post since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
myNumber.ToString("#,###");
Member Avatar
HankReardon
Light Poster
37 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Momerath
Senior Poster
3,830 posts since Aug 2010
Reputation Points: 1,327 [?]
Q&As Helped to Solve: 664 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article