C# Decimal.TryParse 不解析我的十进制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11311570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:21:28  来源:igfitidea点击:

Decimal.TryParse doesn't parse my decimal value

c#decimaltryparse

提问by cadi2108

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

当我尝试转换 0.1 之类的东西(来自文本框中的用户)时,我的值 b 始终为 false。

bool b = Decimal.TryParse("0.1", out value);

How can it be here to work?

怎么会在这里工作?

采纳答案by Matt Roberts

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

参加聚会为时已晚,但我打算建议强制文化为 en-US 但 Invariant 是更好的 sln

decimal value;
bool b = Decimal.TryParse("0.1", NumberStyles.Any, new CultureInfo("en-US"), out value);

回答by burning_LEGION

Use Culturein overload method

Culture在重载方法中使用

回答by Guffa

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

指定解析的区域性。您当前的文化使用了一些不同的数字格式,可能是0,1.

This will successfully parse the string:

这将成功解析字符串:

bool b = Decimal.TryParse("0.1", NumberStyles.Any, CultureInfo.InvariantCulture, out value);