I have a string value for amount that is coming from the DB. The local culture on my system is Portuguese(pt-br). As a result, the amount with decimal values is read as, for ex: 3,4 for 3.4. I need to parse this in such a way that it displays 3.4 but instead no matter what i try I'm getting 34. I have searched every where for a solution and have tried implementing the below, but in vain.
//here row[item.columnName] is the row of the DataTable which I'm looping
Solution 1:
Double amt = Double.Parse(Convert.ToString(row[item.columnName]), CultureInfo.InvariantCulture);
Solution 2:
CultureInfo usCulture = new CultureInfo("en-US");
NumberFormatInfo dbNumberFormat = usCulture.NumberFormat;
Double amt = Double.Parse(Convert.ToString(row[item.columnName]), dbNumberFormat);
Neither of them seem to work.Can some one please suggest if there is any other way I can achieve this?
EDIT:
Turns out that the value I am getting from the DB is of type decimal so I changed my LOC to below.
decimal d = decimal.Parse(Convert.ToString(row[item.columnName]),new System.Globalization.CultureInfo("pt-BR", false));
It still doesn't seem to work and I just don't see where I'm going wrong. I tried the same thing on DotNetFiddle and it works absolutely fine. Below is the code that I tried.
using System;
public class Program
{
public static void Main()
{
decimal d = decimal.Parse("1,35",new System.Globalization.CultureInfo("pt-BR", false));
Console.WriteLine(d.ToString());
}
}
The result I obtained was 1.35 as expected. What am I doing wrong?