Reputation: 1067
I want to seperate a number with commas. I have tried many ways to do it. But didn't work.
Its already converted to a string and now i want to format the "tot".
GetData getData = new GetData();
string tot = Convert.ToString(getData.Total_Extra(month));
string totVal = (tot).ToString("N",new CultureInfo("en-US"));
LB2.Text = tot.ToString();
Upvotes: 0
Views: 173
Reputation: 27322
You can convert your tot
string to a numeric value and then use string.Format
to get the desired format:
string tot = "19950000";
string output = string.Format("{0:n2}", Convert.ToInt32(tot));
Debug.WriteLine(output); //19,950,000.00 on my machine
or alternatively:
string output2 = Convert.ToInt32(tot).ToString("n2");
These are both culture specific, so might display differently on different users machines (Indian culture will display 1,99,50,000.00
for example).
If you want to force the three digit comma grouping then you can specify a culture to use:
string output2 = Convert.ToInt32(tot).ToString("n2", CultureInfo.CreateSpecificCulture("en-GB"));
//19,950,000.00 on any machine
It sounds like your tot
may not be a numeric value, so you should check this before trying to format it:
string tot = "19950000";
int totInt;
if (Int32.TryParse(tot, out totInt))
{
string output = totInt.ToString("n2", CultureInfo.CreateSpecificCulture("en-GB"));
MessageBox.Show(output);
}
else
{
MessageBox.Show("tot could not be parsed to an Int32");
}
Upvotes: 2