0

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?

Akshatha
  • 592
  • 1
  • 11
  • 28
  • 1
    Can you clarify: Exactly what string is coming from `Convert.ToString(row[item.columnName])` ? If it has a comma in it, then parsing it as InvariantCulture of course won't work. You'd have to use the PT-br culture. – Matthew Watson Oct 12 '15 at 10:57
  • Is `row[item.columnName].GetType()` already a numerical type? – Jeppe Stig Nielsen Oct 12 '15 at 11:03
  • 1
    Do you really store string in your db? I doubt, because if so you won't need `Convert.ToString` call. So what is the type of your db column? – Ivan Stoev Oct 12 '15 at 11:08

4 Answers4

0

You can try like this:

var x = decimal.Parse("yourDecimal", new NumberFormatInfo() { NumberDecimalSeparator = "," });
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You can use potrugal culture info

CultureInfo usCulture = new CultureInfo("pt-PT", false);
NumberFormatInfo dbNumberFormat = usCulture.NumberFormat;
Double amt = Double.Parse(Convert.ToString(row[item.columnName]), dbNumberFormat);

or

Double amt = Double.Parse(Convert.ToString(row[item.columnName]), new CultureInfo("pt-PT", false));
  • This does not help. It gives me the value as 3,4 whereas I am looking for 3.4 – Akshatha Oct 12 '15 at 11:55
  • hi Akshatha, I assume your DB as well as app server are in pt-br, hence at loading you might have to convert to international or english culture. If you need to print the double to screen you will have to convert at the tome of display Double amt = Double.Parse("1.233,4", new CultureInfo("pt-BR", false)); Console.WriteLine(amt); Console.WriteLine(amt.ToString(new CultureInfo("pt-BR", false))); Console.WriteLine(amt.ToString(new CultureInfo("en", false))); This works for me – Mustahsan Abdullah Oct 12 '15 at 13:35
0

If you get the double values from the database, then this is not a parsing problem. In Solution 1 you convert the correct double values to string (without specifying any culture, so the string will be formatted by the Portuguese format), and then you parse the Portuguese strings with invariant culture, where , is a thousand separator.

You should simply set the current culture English if you want to format the numbers with the English culture:

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

Or, just specify the culture when you convert the double to string:

var en = CultureInfo.GetCultureInfo("en-US");
// ...
((double)row[item.columnName]).ToString(en);
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
0

Maybe row[item.columnName] is already a double? What does row[item.columnName].GetType() return?

There is no reason to take a (boxed) double and format it into a string (without specifying culture) and then parse that string back to a double (specifying the culture). Even if Convert.ToString has an overload that takes a IFormatProvider (which could be a CultureInfo) as its second parameter.

Maybe

double amt = (double)row[item.columnName];

will work? If you have a decimal, use either decimal amt = (decimal)row[item.columnName]; or double amt = (double)(decimal)row[item.columnName];.

If row[item.columnName].GetType() is really String, cast to that:

double amt = Double.Parse((string)row[item.columnName], someGoodCulture);

I personally dislike Convert.ToString since it is hard to see if it just down-casts a string already present, or it formats some IFormattable instance (such as a double or a decimal) according to the current culture of the current thread, or simply calls .ToString() on some arbitrary object.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181