Manuel Hoffmann
Manuel Hoffmann

Reputation: 547

Converter doesn't convert culture number format?

I have a string number in my C# program, it's "6.967.015". That's the german number format where "." seperates the 1000-groups and "," is the decimal seperator.

I made a function that uses Double.TryParse to convert the text to a double value. It works fine:

        if (Double.TryParse(text, out result))
        {
            return result;
        }
        else
        {
            return defaultValue;
        }

Now I needed a second type to be convertable and instead of copying the function, I wanted to make a function that accepts the type (don't mind the Type argument instead of generic <T> function, it's for integrity reasons).

    public static object DefaultIfNotValid(this object value, Type type, object defaultValue)
    {
        var converter = TypeDescriptor.GetConverter(type);
        object convertedValue;

        if (converter != null)
        {
            try
            {
                convertedValue = converter.ConvertFrom(value);
                return convertedValue;
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        return defaultValue;
    }

The problem with this code is that it works for "123" but it doesn't work for the above number "6.967.015". The Convert function throws "6.967.015 is not a valid value for double". I tried giving the function a culture parameter to see if that was the problem, but that didn't work either (I passed null for the TypeDescriptorContext).

converter.ConvertFrom(null, new CultureInfo("de-de"), value);

That still threw the same exception. I also tried a custom culture with explicitly set group and decimal seperator, but still the same issue.

Maybe it's not even the culture but something else that the converter doesn't like about the value.

Does anyone know what could be the cause of this? Even though the string converts just fine with an explicit double.TryParse call.

Upvotes: 1

Views: 294

Answers (2)

Fabio
Fabio

Reputation: 32445

If TryParse method works use "method overload", default value for failed converting will be target type indicator in your case
Create own converting method for every type you want to convert

public static double DefaultIfNotValid(this string value, double defaultValue)
{
    double temp = 0;
    if (double.TryParse(value, out temp) == true)
        return temp;
    return defaultValue;
}

public static int DefaultIfNotValid(this string value, int defaultValue)
{
    int temp = 0;
    if (int.TryParse(value, out temp) == true)
        return temp;
    return defaultValue;
}

Then using seems not so painful

string rawValue = "6.967.015";
double defaultValue = 9.9;
double parsedValue = rawValue.DefaultIfNotValid(defaultValue);

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

You can try like this:

var nf = new NumberFormatInfo{NumberDecimalSeparator = ",", NumberGroupSeparator = "."};
var convertedValue = Convert.ToDouble(value, nf);

Upvotes: 4

Related Questions