Reputation: 3
In my ASP.net C# application, I have to retrieve values from different databases and stored them in string variables.
One of the variables is used to store Product Amount(pd_amnt) from Italy. So there the values are in the form of "1.000.000.000,00" but I need to replace all the (.) to (,) then the values are in the form of "1,000,000,000.00". To solve this issue I used Replace(), but it failed because nobody can expect how many commas (,) will come for a particular string value.
How to solve this issue? Is there any separate method for the same in C# .NET?
protected void btnSub_Click(object sender, EventArgs e)
{
//Try1
double value = 1.234567890;
Response.Write(value.ToString("#,#", CultureInfo.InvariantCulture));
Response.Write(String.Format(CultureInfo.InvariantCulture,"{0:#,#}", value));
//Try2
string a = txtvalue.Text;
string b= a.ToString(new CultureInfo("en-US"));
txtConverted.Text = b.ToString();
}
Upvotes: 0
Views: 3364
Reputation: 27944
You can use the C or c for formatting currencies:
string.Format("{0,C2}", value);
string.Format(new System.Globalization.CultureInfo("en-GB"), "{0:C}", value)
Or with ToString and culture info:
Console.WriteLine(value.ToString("C2", CultureInfo.CreateSpecificCulture("da-DK"));
The usage of , or . is depending on the given culture you are formatting. You can give formatting rules for these.
Upvotes: 1