Why double.TryParse(“0.0000”, out doubleValue) returns false ?

后端 未结 9 626
一个人的身影
一个人的身影 2021-02-03 20:38

I am trying to parse string \"0.0000\" with double.TryParse() but I have no idea why would it return false in this particular example. When I pass integer-like stri

9条回答
  •  遥遥无期
    2021-02-03 21:16

    Almost certainly the problem is that Thread.CurrentCulture does not use dot as the decimal separator.

    If you know that the number will be always formatted with dot as the decimal separator, use this code that utilizes the other overload of double.TryParse:

    style = NumberStyles.Float | NumberStyles.AllowThousands;
    culture = CultureInfo.InvariantCulture;
    float num;
    if (double.TryParse("0.0000", style, culture, out num)) {
        // whatever
    }
    

提交回复
热议问题