Convert string to double do not respect current number decimal separator

I'm trying to convert a string representing a double from invariant culture to a double in current culture representation, I'm concerned with how to get the new double representation to use the current number decimal separator of Current Culture.

I used the code below for the conversion :

public static double ConvertToDouble(this object inputVal, bool useCurrentCulture = false)
{
    string currentSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
    string invariantSep = CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator;
    if (inputVal.GetType() == typeof(string))
    {
        if (!currentSep.Equals(invariantSep))
        {
            inputVal = (inputVal as string).Replace(invariantSep, currentSep);
        }
    }
    if (useCurrentCulture)
        return Convert.ToDouble(inputVal, CultureInfo.CurrentCulture);
    else
        return Convert.ToDouble(inputVal);
}

But the above code always gives me a double with ".", although I use the CurrentCulture for example French supposed to give me a double with comma (",").

Many thanks in advance for any hint.

FreeDev

Jon Skeet
people
quotationmark

But the above code always gives me a double with "." as the NumberDecimalSeparator

No, it returns a double. A double is just a number. It doesn't have a NumberDecimalSeparator... only a culture does, and that's only applied when converting to or from strings. Talking about the separator for a double is like talking about whether an int is in decimal or hex - there's no such concept. 0x10 and 16 are the same value, represented by the same bits.

It's not really clear what you're trying to do, but it's crucial to understand the difference between what's present in a textual representation, and what's inherent to the data value itself. You should care about the separator when parsing or formatting - but after you've parsed to a double, that information is gone.

people

See more on this question at Stackoverflow