Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a label with numbers in it (example: 15200) and I would like it to have commas in it since sometimes the number will be quite long.

 

I'm sure this is very simple to do because I had done it once, but I can't find the code for it anymore.

 

Thanks.

Posted

Sounds like what I wanted to for an application long time ago. I could never find any ready made code for it (though there probably is something, dealing with formating in some class).

 

Can't you do a loop that inserts a comma into the string at appropiorate places WHILE we're not at the end of the string. And it might be easier code-wise to reverse the string before doing it so one loops form beginning to end so that the commas ends up in the right places(so we don't need to know anything about the number to still insert into the right place), then reverse it again so it's back to normal. Well atleast that's what you could do if you want to have them in groups of three(say: "1284"->"1,284", "1284999"->"1,284,999" etc).

 

If that's something you need lemme know and I'll try to find the code snippet of mine.

Posted (edited)

Hope this helps.

 

Hmm, there is probably a better way to do this, because I made this off the top of my head, but :

 

vbcode:

Dim this As Integer = 1010101777
Dim mystring As String = System.Convert.ToString(this)
For x As Integer = mystring.Length - 3 To 1 Step -3
     mystring = mystring.Insert(x, ",")
Next

 

c++ code:

int this1 = 556654;
System::String ^ mystring;
mystring = System::Convert::ToString(this1);
for(int x = mystring->Length-3; x > 0; x -= 3)
{
   mystring = mystring->Insert(x,",");
}

 

 

This will do the job for you. I was surprised it actually works, but it does.

 

Edit: Oh yeah, you probably know this but the "this" and "this1" are just used as an example, you can use any numbers you want.

 

Sorry I don't know how to do for loops in C# if that's what your using, but you should get the idea.

 

There I go again, making functions for thing that already exist(tostring("N")).

Edited by Ralphzehunter
Posted

uses current thread culture

Dim this As Integer = 1010101777
Console.WriteLine(this.ToString("N"));

 

if the culture is en-us

yeilds

1,010,101,777

 

see help: Standard Numeric Format Strings

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

i have tried a few things trying to use your code Joe Mamma but it won't put the commas...

 

the label that has the number I want commas in is called "lblExpNeeded" (lblExpNeeded.Text)

Posted

I dont know. . . this:

 

 [size=2]
[/size][color=black][size=2]Private[/size][size=2]Sub[/size][size=2] Form1_Load([/size][size=2]ByVal[/size][size=2] sender [/size][size=2]As[/size][size=2]Object[/size][size=2], _[/size][/color]
[color=black][size=2]ByVal[/size][size=2] e [/size][size=2]As[/size][size=2] System.EventArgs) [/size][size=2]Handles[/size][size=2]MyBase[/size][/color][size=2][color=black].Load[/color][/size]
[color=black][size=2]Dim[/size][size=2] i [/size][size=2]As[/size][size=2]Long[/size][/color][size=2][color=black] = 10020020020[/color][/size]
[size=2][color=black]TextBox1.Text = i.ToString("N")[/color][/size]
[color=black][size=2]End[/size][size=2][color=#0000ff][color=black]Sub

[/color][/color][/size][/color]

yields

 

10,020,020,020.00

for me. :confused:

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted (edited)

An alternative is regex, but if I had to choose between regex and jm's solution, I'd go with jm's.

 

string input = "1234567890";
System.Text.StringBuilder output = new System.Text.StringBuilder();
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(input, @"^(\d{1,3})(\d{3})*$");
if(match.Success)
{
output.AppendFormat("{0}", match.Groups[1].Captures[0].Value);
foreach(System.Text.RegularExpressions.Capture capture in match.Groups[2].Captures)
	output.AppendFormat(",{0}", capture.Value);
}
//outputs "1,234,567,890"
Console.WriteLine(output);

Edited by HJB417
  • Leaders
Posted

This works for me:

 

number.ToString("#,###")

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • *Experts*
Posted
How about FormatNumber(123456789)...There are lots of preformated functions with the ability to customize formats in the Format Class. Additionally, these formats take into consideration the user's culture info, ie. commas or spaces for thousands seperators and dots or commas for decimal seperators.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...