Double.TryParse()忽略NumberFormatInfo.NumberGroupSizes? [英] Double.TryParse() ignores NumberFormatInfo.NumberGroupSizes?

查看:120
本文介绍了Double.TryParse()忽略NumberFormatInfo.NumberGroupSizes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我想知道我是否缺少某些东西, code> 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决定,每个组中的数字由$GroupSizes和CurrencyGroupSizes属性$ b $确定为
b NumberFormatInfo。


允许数千包含在NumberStyles.Any中。 NumberGroupSizes是3为我的文化。这只是Double.Parse中的一个错误吗?似乎不太可能,但我无法察觉到我在做错什么...

解决方案

这只是意味着输入字符串可以包含零个或多个 NumberFormatInfo.NumberGroupSeparator 的实例。该分隔符可用于分离任意大小的数字组;不只是数千。当将小数形式格式化为字符串时,将使用 NumberFormatInfo.NumberGroupSeparator NumberFormatInfo.NumberGroupSizes 。使用反射器看起来像 NumberGroupSeparator 仅用于确定字符是否是分隔符,如果是,则跳过它。 NumberGroupSizes 根本不使用。



如果要验证字符串,可以使用RegEx或写一个方法来做到这一点。这是我刚刚入侵的一个:

  string number =102,000,000.80; 
var parts = number.Split(','); (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]);
}
}

//尊重文化
static Boolean CheckThousands(String value)
{
String [] parts = value.Split (new string [] {CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator},StringSplitOptions.None);
foreach(部分字符串)
{
int length = part.Length;
if(CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes.Contains(length)== false)
{
return false;
}
}

返回true;
}


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

Expected output would be nothing... "1,2,3" shouldn't parse as a double. However it does. According to the .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 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....

解决方案

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

这篇关于Double.TryParse()忽略NumberFormatInfo.NumberGroupSizes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆