C# float.tryparse for French Culture

后端 未结 3 433
感情败类
感情败类 2020-12-11 01:47

I have a user input which can contain float values ranging from : 3.06 OR 3,06 The culture we are in is French and thus when the user inputs 3.06 and I run a float.tryParse

3条回答
  •  春和景丽
    2020-12-11 02:48

    You can use the overload that takes a format provider. You can pass through a French culture info:

    string value;
    NumberStyles style;
    CultureInfo culture;
    double number;
    
    value = "1345,978";
    style = NumberStyles.AllowDecimalPoint;
    culture = CultureInfo.CreateSpecificCulture("fr-FR");
    if (Double.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    // Displays:
    //       Converted '1345,978' to 1345.978.
    

提交回复
热议问题