Double.TryParse()忽略NumberFormatInfo.NumberGroupSizes?TryParse、Double、NumberGroupSizes、NumberFormatIn

2023-09-05 00:22:03 作者:天边シ深海

我想知道如果我失去了一些东西或不...我在标准伟大的英国文化运行。

I'd like to know if I'm missing something or not... I'm running under the standard Great British culture.

Double result = 0;
if (Double.TryParse("1,2,3", NumberStyles.Any, CultureInfo.CurrentCulture, out result))
{
   Console.WriteLine(result);
}

预计产量将是什么...1,2,3不应该解析为一个双。但是它的作用。按照.NET 2.0 MSDN文档

AllowThousands表示该数字串可以组   分离器;例如,分离数百从数千。   有效的组分隔符被确定   的NumberGroupSeparator和CurrencyGroupSeparator性能   的NumberFormatInfo和数字每组中的数目被确定   通过的NumberGroupSizes和CurrencyGroupSizes属性   的NumberFormatInfo。

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.

允许数以千计的包括在NumberStyles.Any。该NumberGroupSizes是3对我的培养。这是在Double.Parse只是一个错误?似乎不太可能,但我不能当场我在做什么错了......

Allow thousands is included in NumberStyles.Any. The NumberGroupSizes is 3 for my culture. Is this just a bug in the Double.Parse? seems unlikely but I can't spot what I'm doing wrong....

推荐答案

这仅仅意味着输入字符串可以包含 NumberFormatInfo.NumberGroupSeparator 的零个或多个实例。该分离器可用于任何大小的数字的不同组;不只是数千人。 NumberFormatInfo.NumberGroupSeparator 和 NumberFormatInfo.NumberGroupSizes 时,被作为字符串格式化小数使用。使用反射好像 NumberGroupSeparator 仅用于确定是否该字符是一个分离器,并且如果是,则它被跳过。 NumberGroupSizes 未使用的。

It just means the input string can contain zero or more instances of NumberFormatInfo.NumberGroupSeparator. This separator can be used to separate groups of numbers of any size; not just thousands. NumberFormatInfo.NumberGroupSeparator and NumberFormatInfo.NumberGroupSizes are used when formatting decimals as strings. Using Reflector it seems like NumberGroupSeparator is only used to determine if the character is a separator, and if it is, it is skipped. NumberGroupSizes is not used at all.

如果您想验证字符串,你可以这样做使用正则表达式或写入的方法来做到这一点。这里有一个我砍死在一起:

If you want to validate the string, you could do so using RegEx or write a method to do so. Here's one I just hacked together:

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