C# float.tryparse for French Culture

后端 未结 3 432
感情败类
感情败类 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:27

    float usedAmount;
    
    // try parsing with "fr-FR" first
    bool success = float.TryParse(inputUsedAmount.Value,
                                  NumberStyles.Float | NumberStyles.AllowThousands,
                                  CultureInfo.GetCultureInfo("fr-FR"),
                                  out usedAmount);
    
    if (!success)
    {
        // parsing with "fr-FR" failed so try parsing with InvariantCulture
        success = float.TryParse(inputUsedAmount.Value,
                                 NumberStyles.Float | NumberStyles.AllowThousands,
                                 CultureInfo.InvariantCulture,
                                 out usedAmount);
    }
    
    if (!success)
    {
        // parsing failed with both "fr-FR" and InvariantCulture
    }
    

提交回复
热议问题