简体   繁体   中英

C# parse value from string

I would like to parse string value like this :

.02234 
-.23455 
-1.23345 
2. 
.3 

but i get an FormatException

 for (int i = 1; i < 7; i++)
 {
     var item =  Double.Parse(reader.ReadLine(44).Substring(8 * i, 8));
     richTextBox1.Text += item.ToString() + "\n";  
 }

在此输入图像描述

the problem that i should convert this numbers like "0.2" or "-.0541" to double or any value type to work with it !!!

You're passing a string that isn't a double, something like 1.a or 1. .3 (1 string representing 2 numbers)

You can use Double.TryParse() and it will not throw an exception but return true/false if it was successful or not. It might make the flow easer.

Since you've made a comment that , is the decimal separator in your locale, there is a better option than doing a string-replace of . to , ; tell the Double.Parse() method to use a different number format.

See the MSDN doc for Parse(String s) . Especially, note the following:

The s parameter is interpreted using the formatting information in a NumberFormatInfo object that is initialized for the current thread culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, call the Double.Parse(String, IFormatProvider) or Double.Parse(String, NumberStyles, IFormatProvider) method.

Assuming your current thread culture is using a number format that considers , to be the decimal separator (French/France fr-FR , for example), you must pass an IFormatProvider to the Parse() method that defines . as the decimal separator. Conveniently, the "no culture in particular" culture, CultureInfo.InvariantCulture , does just this.

So this code should parse successfully:

for (int i = 1; i < 7; i++)
{
    // Assume the substring of ReadLine() contains "-.23455", for example
    var item = Double.Parse(reader.ReadLine(44).Substring(8 * i, 8), CultureInfo.InvariantCulture);
    richTextBox1.Text += item.ToString() + "\n";  
}
reader.ReadLine(44).Substring(8 * i, 8).ToString()

You won't need the .ToString(). Verify that this actually returns the value you're looking for.

The format exception is throw because the value it's trying to parse is not valid! My first guess is that the argument you're passing is not in fact a double.

Might try to split up your calls, and toss in a breakpoint to see what's actually going on.

 for (int i = 1; i < 7; i++)
 {
     string textNum = reader.ReadLine(44).Substring(8 * i, 8);
     // add a breakpoint on the next line, then look at textNum. I bet it's not what you hoped.
     double item =  double.Parse(textNum);
     richTextBox1.Text += string.Format("{0}\n", item);  
 }

Use Double.TryParse() and test to see if the value was successfully parsed into your local variable like this:

for (int i = 1; i < 7; i++)
{
    double value = 0;
    string input = reader.ReadLine(44).Substring(8 * i, 8);

    if (Double.TryParse(input, out value))
    {
        richTextBox1.Text += value.ToString() + "\n";  
    }
    else
    {
        richTextBox1.Text = "Invalid double entered.";
    }
}

i just find a solution to the problem ( replace to dot with comma ) :

    public Double[] GridValues(int fromline) 
    {
        Double[] values = new Double[7];
        for (int i = 1; i < 7; i++)
        {
            string input = ReadLine(fromline).Substring(8 * i, 8).Replace(".", ",");
            values[i-1] = double.Parse(input);
        }

        return values;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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