-2

I have a variable of type Long i.e.

long quantity=1000;

I want to display it like 1,000 in Grid (Must need commas)

How do i achieve this?

I am using a Telerik Grid and I am binding the data as follows:

columns.Bound(tempProductList => tempProductList.tempProductListQuantity) .Title("Quantity")
musefan
  • 47,875
  • 21
  • 135
  • 185
user2450398
  • 175
  • 1
  • 7
  • 16
  • 1
    What Grid exactly? Show the relevant code. – Jon Aug 14 '13 at 08:46
  • @Jon, its Telerik Grid. Here is my sample code for Telerik Grid columns.Bound(tempProductList => tempProductList.tempProductListQuantity) .Title("Quantity") – user2450398 Aug 14 '13 at 08:47
  • 1
    possible duplicate of [.NET String.Format() to add commas in thousands place for a number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – Richard Ev Aug 14 '13 at 08:51
  • Do you want to force the comma irrespective of culture? Or do you want it to display the correct character for the current culture? e.g. `en-US = "1,234"` and `fr-CH = "1'234"`, etc. – musefan Aug 14 '13 at 08:57

3 Answers3

3

Here you have a list of all the standard numeric formats. I think "N" is the one you want.

long l = 1234;
string s  = l.ToString("N0"); //gives "1,234"

The "0" after the format specifier is the number of desired decimal places (usually 2 by default).

Note that this version is culture-sensitive, i.e., in my country, we use dots (".") as thousand separators, so the actual returned value will be "1.234" instead of the "1,234". If this is desired behaviour, just leave it as is, but if you need to use commas always, then you should specify a culture as a parameter to the ToString method, like

l.ToString("N0", CultureInfo.InvariantCulture); //always return "1,234"
SWeko
  • 30,434
  • 10
  • 71
  • 106
0

You could create a Custom Culture that will allow you to specify the thousand separator.

From this article:

//Create first the format provider the String.Format 
//will use to format our string
CultureInfo cultureToUse = new CultureInfo("fi-FI");
Console.WriteLine("Using the following CultureInfor " + 
                  cultureToUse.Name);

//Now practice some decimal numbers 
//Here we override the culture specific formattings
cultureToUse.NumberFormat.CurrencyDecimalDigits = 3;
cultureToUse.NumberFormat.NumberDecimalDigits = 3;
cultureToUse.NumberFormat.NumberGroupSeparator = " ";
cultureToUse.NumberFormat.CurrencySymbol = "euro";
cultureToUse.NumberFormat.NumberDecimalSeparator = ",";

Next you would need to use this culture when formatting the numbers. You could do the formattin gby hand but you could also assign the culture to the Current(UI)Culture property of the current thread.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Why go through all this trouble when you can just specify the format in the `ToString` function? – musefan Aug 14 '13 at 08:53
  • This is a nice global solution if you need to change **all** number formating in one fell swoop. – SWeko Aug 14 '13 at 08:54
  • Also, this doesn't even answer the question. All it shows is how to create a CultureInfo, you do not demonstrate applying it anywhere, which is the key factor. Especially considering there will already be a default culture info set which will use some sort of thousands separator anyway.. sorry, have to vote you down at the minute – musefan Aug 14 '13 at 09:04
0

If you want to consider the international point of view, there will not be always commas before the decimal part. ToString function will give you what you want.

(1000.0).ToString("N",new CultureInfo("en-US")) = 1,000.00

(1000.0).ToString("N",new CultureInfo("is-IS")) = 1.000,00

Burak Keceli
  • 933
  • 1
  • 15
  • 31