c# - Double.TryParse 千位分隔符返回意外结果

标签 c# .net cultureinfo

我刚刚遇到了一些非常奇怪的事情,只是想知道我是否遗漏了什么。

我试图将一个字符串(带有千位分隔符)解析为 double 字符串,并发现了以下问题。

CultureInfo ci = CultureInfo.CurrentCulture; // en-ZA
string numberGroupSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; //numberGroupSeparator = ,
string numberDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;//numberDecimalSeparator = .
string strValue = "242,445.24";
double try1;
double try2;
bool btry1 = Double.TryParse(strValue, out try1); //try1 = 242445.24 : btry1 = true
bool btry2 = Double.TryParse(strValue, NumberStyles.Any, null, out try2); //try2 = 0.0 : btry2 = false <- STRANGE
double try3 = Convert.ToDouble(strValue); //try3 = 242445.24

现在我不只是想使用 Convert.ToDouble 的原因是科学记数法之前给我带来了一些问题。

有人知道为什么会这样吗?

编辑:

我已经更新了我当前的文化信息。

最佳答案

它在我的机器上按预期工作,所以我相信它与当前文化有关。尝试在 TryParse

中使用 CultureInfo.InvariantCulture 而不是 null
Double.TryParse(strValue, NumberStyles.Any,CultureInfo.InvariantCulture, out try2);

您当前指定的文化 en-ZA 失败,我尝试了以下代码并且 try2 保持 0.0

 Double.TryParse(strValue, NumberStyles.Any,new CultureInfo("en-ZA"), out try2); 

关于c# - Double.TryParse 千位分隔符返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12300194/

相关文章:

c# - 使用 XmlReader.Create(uri) 防止或处理超时

.net - 将 .net 控件呈现为字符串并触发事件

c# - 在 SQL Server 中比较字符串

c# - String.Format 没有正确转换阿拉伯语的整数

C#:非标志枚举,有没有更简单的方法来检查枚举以匹配多个值之一?

c# - 使用 MahApps Metro 主题 VS2013

c# - 如何使用 .NET 为 CloudFront 公开的 Amazon S3 生成签名 URL

c# - C# 中的字符串比较性能

时间:: In a dotnet class is there a property that states if the "Current" culture is actual the default culture?

c# - 由于某种原因,这些列从整个数据库添加到 listbox1,尽管只需要从 Students 表中获取