Decimal.TryParse doesn't parse my decimal value

后端 未结 3 568
误落风尘
误落风尘 2021-02-18 15:13

When I tried to convert something like 0.1 (from user in textbox), My value b is always false.

bool b = Decimal.TryParse(\"0.1\", out value);
相关标签:
3条回答
  • 2021-02-18 15:47

    Use Culture in overload method

    0 讨论(0)
  • 2021-02-18 16:07

    Specify the culture for the parsing. Your current culture uses some different number format, probably 0,1.

    This will successfully parse the string:

    bool b = Decimal.TryParse("0.1", NumberStyles.Any, CultureInfo.InvariantCulture, out value);
    
    0 讨论(0)
  • 2021-02-18 16:09

    Too late to the party, but I was going to suggest forcing the culuture to en-US but Invariant is a better sln

    decimal value;
    bool b = Decimal.TryParse("0.1", NumberStyles.Any, new CultureInfo("en-US"), out value);
    
    0 讨论(0)
提交回复
热议问题