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);
            }

Comments

Popular posts from this blog

Embedding PowerBI with ASP.NET Core 2 and Angular(2,4,5)

Handling Exit Event of Console Application in C#

CSS text-overflow ellipsis in the beginning of the the text - Left Ellipsis using CSS in a Flex box