- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道我是否遗漏了什么......我在标准的大不列颠文化下运行。
Double result = 0;
if (Double.TryParse("1,2,3", NumberStyles.Any, CultureInfo.CurrentCulture, out result))
{
Console.WriteLine(result);
}
预期的输出是什么...“1,2,3”不应解析为 double 。然而它确实如此。根据 .NET 2.0 MSDN documentation
AllowThousands Indicates that the numeric string can have group separators; for example, separating the hundreds from the thousands. Valid group separator characters are determined by the NumberGroupSeparator and CurrencyGroupSeparator properties of NumberFormatInfo and the number of digits in each group is determined by the NumberGroupSizes and CurrencyGroupSizes properties of NumberFormatInfo.
Allow thousands 包含在 NumberStyles.Any 中。 NumberGroupSizes 对于我的文化是 3。这只是 Double.Parse 中的一个错误吗?似乎不太可能,但我无法发现我做错了什么....
最佳答案
这只是意味着输入字符串可以包含零个或多个 NumberFormatInfo.NumberGroupSeparator
实例。此分隔符可用于分隔任意大小的数字组;不只是成千上万。 NumberFormatInfo.NumberGroupSeparator
和 NumberFormatInfo.NumberGroupSizes
用于将小数格式化为字符串。使用 Reflector 似乎 NumberGroupSeparator
仅用于确定字符是否为分隔符,如果是,则跳过。 NumberGroupSizes
根本没有使用。
如果要验证字符串,可以使用 RegEx 或编写一个方法来验证字符串。这是我刚刚一起破解的:
string number = "102,000,000.80";
var parts = number.Split(',');
for (int i = 0; i < parts.Length; i++)
{
var len = parts[i].Length;
if ((len != 3) && (i == parts.Length - 1) && (parts[i].IndexOf('.') != 3))
{
Console.WriteLine("error");
}
else
{
Console.WriteLine(parts[i]);
}
}
// Respecting Culture
static Boolean CheckThousands(String value)
{
String[] parts = value.Split(new string[] { CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator }, StringSplitOptions.None);
foreach (String part in parts)
{
int length = part.Length;
if (CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes.Contains(length) == false)
{
return false;
}
}
return true;
}
关于c# - Double.TryParse() 忽略 NumberFormatInfo.NumberGroupSizes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8883801/
C# 中的 Single.TryParse 和 float.TryParse 有什么区别? float a, b; float.TryParse("0.01", out a); Single.TryP
在 C# 中,为什么 int.TryParse 不能解析数字分组(但 double.TryParse 可以)? int i1 = 13579; string si1 =
我已按值或其名称将代码写到TryParse枚举,如下所示。如何扩展此代码以包含具有Flags属性的枚举? public static bool TryParse(this T enum_typ
我构建了一个玩具电子表格来帮助学习 F#。当我处理新单元格的文本时,我将其存储为可识别类型。为了解析它,我觉得我应该能够做类似的事情: let cv = mat
我有一个枚举(假设是 DemoEnum),我想解析该枚举的值。我正在写 DemoEnum value; if(DemoEnum.TryParse("input", out value)) { t
我构建了一个玩具电子表格来帮助学习 F#。当我处理新单元格的文本时,我将其存储为可识别类型。为了解析它,我觉得我应该能够做类似的事情: let cv = mat
我有以下代码。 using System; class program { static void Main() { string StrNumber = "100TG
这个问题在这里已经有了答案: How to use int.TryParse with nullable int? [duplicate] (5 个答案) 关闭 4 年前。 我想制作一个方法 int
今天我读了一篇文章,其中写道我们应该始终使用 TryParse(string, out MMM) 进行转换而不是 Convert.ToMMM()。 我同意这篇文章,但在那之后我陷入了一种情况。 当字符
我需要验证某些变量是否可以创建有效的日期时间,如果可以,则在不抛出异常的情况下忽略它。 我有以下代码 int y, m, d, h, mi, s, tz; ogrFeature.GetFieldAs
我正在尝试运行 NUnit 测试来搜索字符串中的数字。我正在使用 TryParse,但它给了我一个错误:Error1:'int.TryParse(string, out int)' 的最佳重载方法匹配
我需要检查一个对象是否可以转换为特定的数据类型,并想出了这个: public static bool TryParseAll(System.Type typeToConvert, object val
Facebook API 函数 FacebookOAuthResult.TryParse() 未解析。我为 TryParse 函数提供了正确的输入参数。我已经在该项目的 Visual Studio 引
我是编程新手,我正在尝试创建一个循环,其中 while 条件基于变量,但我不知道它的正确语法。这就是我想要做的,即使它可能不被允许......我认为。 using System; using Syst
我知道它不会抛出异常,因此它可能会明显更快,而且,您很可能使用它来将输入转换为您可以使用的数据,所以我认为它没有被这样使用通常会在性能方面产生如此大的差异。 无论如何,我看到的例子都是 if/else
我想尝试将 string 解析为 DateTime?,如果失败,则将值设置为 null。我能想到的唯一方法如下,但它看起来不是很整洁。 DateTime temp; DateTime? whatIAc
在我的应用程序中,我有一个文本框 - txtDiscount,管理员可以在其中为某个用户设置折扣百分比,也可以不设置。在后端我想保存数据所以现在我有这个: double? discount = nul
我尝试按如下方式解析来自外部系统的日期时间戳: DateTime expiration; DateTime.TryParse("2011-04-28T14:00:00", out expiration
我在让 TryParse 为我正常工作时遇到问题。我有一个值列表,我几乎可以肯定这些值是有效的(因为它们来 self 们系统中的另一个组件),但我想确保有适当的错误处理。 这是我的值(value)观的
我想覆盖 bool 的 TryParse 方法来接受"is"和“否”。我知道我想使用的方法(如下),但我不知道如何覆盖 bool 的方法。 ... bool TryParse(string value
我是一名优秀的程序员,十分优秀!