Generic string analysis in float

In the environment in which my program will be launched, people use "," and "." . as decimal separators randomly on a PC with "," and "." . separators.

How would you implement such a floatparse (string) function?

I tried the following:

    try
    {
        d = float.Parse(s);
    }
    catch
    {
        try
        {
            d = float.Parse(s.Replace(".", ","));
        }
        catch
        {
            d = float.Parse(s.Replace(",", "."));
        }
    }

This does not work. And when I debug, it turns out that he parses it wrong for the first time, thinking that "." is a separator for thousands (for example, 100.000.000.0).

I am noob in C #, so I hope there is a less complicated solution, and then :-)

NB: People are going to use both. and ',' on a PC with different separator settings.

+5
source share
3 answers

, , :

string s = "1,23";  // or s = "1.23";

s = s.Replace(',', '.');
double d = double.Parse(s, CultureInfo.InvariantCulture);

:

  • . Replace (otherChar, myChar).
  • , , RegEx, . . , , .
  • , TryParse. .
+9

CultureInfo.CurrentCulture, , . , , , double.Parse(). "8,5", double.Parse( "8,5" ) 8.5. "8.5", 85.

, , . , . , , , Masked .

, - , KeyDown . CultureInfo.CurrentCulture.NumberFormat.

, , . , , CultureInfo . , User.Brain.CultureInfo .NET: P

+2

-

float ConvertToFloat(string value)
{
    float result;

    var converted  = float.TryParse(value, out result);

    if (converted) return result;

    converted = float.TryParse(value.Replace(".", ",")), 
                               out result);

    if (converted) return result;

    return float.NaN;
}

        Console.WriteLine(ConvertToFloat("10.10").ToString());
        Console.WriteLine(ConvertToFloat("11,0").ToString());
        Console.WriteLine(ConvertToFloat("12").ToString());
        Console.WriteLine(ConvertToFloat("1 . 10").ToString());

10,1
11
12
NaN

, , , , , . .

float.TryParse(value,
            NumberStyles.Currency,
            CultureInfo.CurrentCulture,
            out result)

:

Console.WriteLine(ConvertToFloat("10,10").ToString());
Console.WriteLine(ConvertToFloat("11,0").ToString());
Console.WriteLine(ConvertToFloat("12").ToString());
Console.WriteLine(ConvertToFloat("1 . 10").ToString());
Console.WriteLine(ConvertToFloat("100.000,1").ToString());

10,1
11
12
110
100000,1

, , "" , , , , .

float ConvertToFloat(string value)
{
    float result;

    var converted = float.TryParse(value,
                                    out result);


    if (converted) return result;

    converted = float.TryParse(value.Replace(".", ","),
                                out result);

    if (converted) return result;

    converted = float.TryParse(value,
                                    NumberStyles.Currency,
                                    CultureInfo.CurrentCulture,
                                    out result);

    return converted ? result : float.NaN;
}

Console.WriteLine(ConvertToFloat("10,10").ToString());
Console.WriteLine(ConvertToFloat("11,0").ToString());
Console.WriteLine(ConvertToFloat("12").ToString());
Console.WriteLine(ConvertToFloat("1 . 10").ToString());
Console.WriteLine(ConvertToFloat("100.000,1").ToString());
Console.WriteLine(ConvertToFloat("asdf").ToString());

10,1
11
12
110
100000,1
NaN
+1

All Articles