tags:

views:

14277

answers:

9

I want to add a comma in the thousands place for a number. String.Format()?

+17  A: 

http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

      ' Format a negative integer or floating-point number in various ways.
  Console.WriteLine("Standard Numeric Format Specifiers")
  s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
                    "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
                    "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
                    "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
                    "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                    "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                    "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
                    "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
                    "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
                    "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                    - 123, - 123.45F)
  Console.WriteLine(s)
Adam Driscoll
+22  A: 
String.Format("{0:n}", 1234);

string.Format("{0:n0}", 9876); // no decimals.
Terrapin
`{0:n}` is giving me unwanted decimal places
Maslow
{0:n0} will remove those
Hafthor
A: 
string.format("{0:#,###,###.##}", MyNumber)

That will give you commas at the thousand, and millions point.

Stephen Wrighton
The ":n" method is better since it should respect the user's locale.
Torlack
This is true, but it's not guaranteed to give you commas at the thousand point because it respect the user's locale.
Stephen Wrighton
@Stephen Wrighton, right back at you: that is true, but it's not guaranteed to respect the user's locale because it uses commas as thousands separator. (As an example, in Portugal the comma is instead the decimal separator.)
ANeves
A: 

Note that the value that you're formatting should be numeric. It doesn't look like it will take a string representation of a number and format is with commas.

+11  A: 
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
+1  A: 
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
p.campbell
A: 

you can use this code in javascript

function GetNumberWithQ(Price) {

    var myNumber = Price.toString(); // Price Is Your Namber Your Number 
       var myResult = "";
        for (var i = myNumber.length - 1; i >= 0; i--)
        {
            myResult = myNumber[i] + myResult;
            if ((myNumber.length - i) % 3 == 0 & i > 0)
                myResult = "," + myResult;
        }

        return myResult;


}
ahmad sarkar
you can use .WithCommas in any imaginary language too, but this is about c#. -1
atamanroman
A: 

I found this to be the simplest way: myString.ToString("N0")

alchemical
A: 

if u want culture specific, you might want to try this.

(19950000.0).ToString("N",new CultureInfo("en-US")) = 19,950,000.00

(19950000.0).ToString("N",new CultureInfo("is-IS")) = 19.950.000,00

note: some cultures use , to mean decimal rather than . so becareful.

prabir