I want to add a comma in the thousands place for a number.
Would String.Format() be the correct path to take? What format would I use?
I want to add a comma in the thousands place for a number.
Would String.Format() be the correct path to take? What format would I use?
String.Format("{0:n}", 1234); // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
String.Format("{0:#,###,###.##}", MyNumber)
That will give you commas at the relevant points.
C# 7.1 (perhaps earlier?) makes this as easy and nice-looking as it should be, with string interpolation:
var jackpot = 1_000_000; // underscore separators in numeric literals also available since C# 7.0
var niceNumberString = $"Jackpot is {jackpot:n}";
var niceMoneyString = $"Jackpot is {jackpot:C}";
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.
You want same Format value and culture specific.
Double value= 1234567;
value.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN"));
Output: 12,34,567
I tried many of the suggestions above but the below work better for me:
string.Format("{0:##,###.00}", myValue)
but this fails when you have values like 0.2014 where it gives .21 For this I use
string.Format("{0:#,##0.00}", myValue)
Try this:
var number = 123456789;
var str = number.ToString("N0");
Result is: "123,456,789"
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))
dt.Rows.Add(1)
dt.Rows.Add(10)
dt.Rows.Add(2)
DataGridView1.DataSource = dt