Decimal.TryParse doesn't parse my decimal valu

2019-04-18 17:52发布

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);

How can it be here to work?

3条回答
聊天终结者
2楼-- · 2019-04-18 18:14

Use Culture in overload method

查看更多
smile是对你的礼貌
3楼-- · 2019-04-18 18:15

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);
查看更多
该账号已被封号
4楼-- · 2019-04-18 18:16

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);
查看更多
登录 后发表回答