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

后端 未结 9 622
一个人的身影
一个人的身影 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:24

    it takes the localization settings at runtime into account... perhaps you are running this on a system where . is not the decimal point but , instead...

    In your specific case I assume you want a fixed culture regardless of the system you are running on with . as the decimal point:

    double.TryParse("0.0000", NumberStyles.Number, CultureInfo.CreateSpecificCulture ("en-US"), out temp)
    

    OR

    double.TryParse("0.0000", NumberStyles.Number,CultureInfo.InvariantCulture, out temp)
    

    Some MSDN reference links:

    • Double.TryParse(String, NumberStyles, IFormatProvider, Double)
    • CultureInfo.CreateSpecificCulture(String)
    • IFormatProvider Interface
    • CultureInfo.InvariantCulture Property

提交回复
热议问题