简体   繁体   English

不能double.Parse字符串

[英]Can't double.Parse string

I have a problem with parsing a string to a double. 我将字符串解析为double时遇到问题。 I have a StreamWriter reading lines from a text file. 我有一个来自文本文件的StreamWriter读取行。 The text file has the following lines: 文本文件包含以下行:

17-09-2012: (100,98)
17-09-2012: (50,57)

Now, I want to use the values from inside the parantheses, add them together and display them in a textbox. 现在,我想使用parantheses中的值,将它们添加到一起并在文本框中显示它们。 So far I have the following: 到目前为止,我有以下内容:

int counter = 0;
double res = 0;
string line;

System.IO.StreamReader file = new System.IO.StreamReader("d:\\test.txt");
while ((line = file.ReadLine()) != null)
{
    string par = Regex.Match(line, @"\(([^)]*)\)").Value;
    double par2 = double.Parse(par);
    res += par2;

    counter++;
}
file.Close();
textBox1.Text = res.ToString();

However, apparently the input string is not in a correct format, which I find rather odd, since the regex should remove everything but the numbers inside the parantheses. 但是,显然输入字符串的格式不正确,我发现这很奇怪,因为正则表达式应该删除除了parantheses中的数字之外的所有内容。 I have even checked for this by writing the string to the textbox without first adding them together, and it showed "100,9850,57" . 我甚至通过将字符串写入文本框而没有先将它们添加到一起来检查这一点,它显示"100,9850,57" So truly, I do not understand, why it cannot convert the string to a double. 所以真的,我不明白,为什么它不能将字符串转换为double。

I hope you can tell me, what I am doing wrong. 我希望你能告诉我,我做错了什么。

你的“par”变量包含一个看起来像这样的字符串:“(100,98)”这就是它无法解析的原因。

将你的正则表达式改为(?<=\\()(([^)]*))(?=\\))

You can try with - based on InvariantCulture 您可以尝试使用 - 基于InvariantCulture

 var culture = CultureInfo.InvariantCulture;
 double result = double.Parse(par , culture);

You can force double.Parse to use a culture that uses , as a decimal separator, like this: 您可以强制double.Parse用采用一种文化,作为小数分隔符,就像这样:

CultureInfo culture = new CultureInfo("de-DE");
double d = double.Parse(par, culture);

That's a good idea anyway, if you want your program to work also on computers with different regional settings. 无论如何,如果您希望您的程序也可以在具有不同区域设置的计算机上工作,那么这是一个好主意。

Setting your regex to (?<=\\()(([^)]*))(?=\\)) and using this helper should solve your problems: 将正则表达式设置为(?<=\\()(([^)]*))(?=\\))并使用此帮助程序可以解决您的问题:

        public static double ParseDouble(string input)
        {
            // unify string (no spaces, only . )
            string output = input.Trim().Replace(" ", "").Replace(",", ".");

            // split it on points
            string[] split = output.Split('.');

            if (split.Count() > 1)
            {
                // take all parts except last
                output = String.Join("", split.Take(split.Count() - 1).ToArray());

                // combine token parts with last part
                output = String.Format("{0}.{1}", output, split.Last());
            }

            // parse double invariant
            double d = Double.Parse(output, CultureInfo.InvariantCulture);
            return d;
        }

I made it work, by making a try, catch: 我试了一下,抓住了:

string par = Regex.Match(line, @"(?<=\()(([^)]*))(?=\))").Value;
                try
                {
                    double par2 = double.Parse(par);
                    res += par2;
                }
                catch
                {
                }

Thanks, everyone for your help. 谢谢,大家帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM