without - string.format c#

.NET String.Format() to add commas in thousands place for a number (14)

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

Below is a good solution in Java though!

NumberFormat fmt = NumberFormat.getCurrencyInstance();

or for a more robust way you may want to get the locale of a particular place, then use as below:

int n=9999999;
Locale locale = new Locale("en", "US");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

US Locale OUTPUT: $9,999,999.00

German Locale output

Locale locale = new Locale("de", "DE");

OUTPUT: 9.999.999,00 €

Indian Locale output

Locale locale = new Locale("de", "DE");

OUTPUT: Rs.9,999,999.00

Estonian Locale output

Locale locale = new Locale("et", "EE");

OUTPUT: 9 999 999 €

As you can see in different outputs you don't have to worry about the separator being a comma or dot or even space you can get the number formatted according to the i18n standards

For example String.Format("{0:0,0}", 1); returns 01, for me is not valid

This works for me

19950000.ToString("#,#", CultureInfo.InvariantCulture));

output 19,950,000

If you 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 be careful.

If you want to show it in DataGridview , you should change its type , because default is String and since you change it to decimal it considers as Number with floating point

Dim dt As DataTable = New DataTable
dt.Columns.Add("col1", GetType(Decimal))

DataGridView1.DataSource = dt

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.

Simpler, using string interpolation instead of String.Format

 $"{12456:n0}"; // 12,456
 $"{12456:n2}"; // 12,456.00

or using yourVariable

 double yourVariable = 12456.0;

The following example displays several values that are formatted by using custom format strings that include zero placeholders.

String.Format("{0:N1}", 29255.0);



result "29,255.0"

String.Format("{0:N2}", 29255.0);



result "29,255.00"

The method I used to not worry anymore about cultures and potential formatting issues is that I formatted it as currency and took out the currency symbol afterwards.

if (decimal.TryParse(tblCell, out result))

  formattedValue = result.ToString("C").Substring(1);

This is the best format. Works in all of those cases:

String.Format( "{0:#,##0.##}", 0 ); // 0
String.Format( "{0:#,##0.##}", 0.5 ); // 0.5 - some of the formats above fail here. 
String.Format( "{0:#,##0.##}", 12314 ); // 12,314
String.Format( "{0:#,##0.##}", 12314.23123 ); // 12,314.23
String.Format( "{0:#,##0.##}", 12314.2 ); // 12,314.2
String.Format( "{0:#,##0.##}", 1231412314.2 ); // 1,231,412,314.2

You can use a function such as this to format numbers and optionally pass in the desired decimal places. If decimal places are not specified it will use two decimal places.

    public static string formatNumber(decimal valueIn=0, int decimalPlaces=2)
        return string.Format("{0:n" + decimalPlaces.ToString() + "}", valueIn);

I use decimal but you can change the type to any other or use an anonymous object. You could also add error checking for negative decimal place values.

String.Format("0,###.###"); also works with decimal places

String.Format("{0:#,###,###.##}", MyNumber)

That will give you commas at the relevant points.

int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));

int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000