Error in parsing Convert.ToDouble or double.Parse for decimal values in C#
I found that one of my client's application showing "0,00" for pricing column of his application, and found that it works find on my pc. Once I traced the error, I found that it fails when parsing price like "1500.00" with Convert.ToDouble or double.Parse. The reason for that is, the particular client using Finnish currency format, which is like "1500,00". As you see the difference is they use "," instead of "." for decimal separation. If you want to convert decimal (money) values in string format, which will universally work in every where, you have to use following code snippet.
string pricetext = "5535.25";
string[] parts = pricetext.Split('.');
double price = Convert.ToDouble(parts[0]);
if (parts.Length == 2)
{
dec = Convert.ToInt32(parts[1]);
price += (dec * 0.01);
}
string pricetext = "5535.25";
string[] parts = pricetext.Split('.');
double price = Convert.ToDouble(parts[0]);
if (parts.Length == 2)
{
dec = Convert.ToInt32(parts[1]);
price += (dec * 0.01);
}
Comments
Post a Comment