Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 26, 2006 1:24 PM
Hi
Im trying to read in values from a file containing numbers that are a mix of "normal" doubles (i.e. 3.23124) and numbers written in scientific notation (i.e. -12e3). I guess this has something to do with the format of the string.
Im trying to feed the values into a custum struct called Matrix, which takes doubles... The code I'm using is:
if(numberOfRows>0 && numberOfColumns>0)
{
Matrix M = new Matrix(numberOfRows,numberOfColumns);
fs.Position = 0;
int i=0;
while ((line = reader.ReadLine()) != null)
{
string[] split = line.Split(new Char[] {' '});
for(int j =0; j<split.Length; j++)
M[i,j]=Double.Parse(split[ i ]);
i++;
}
fs.Close();
return M;
}
On compiling I recieve a "FormatException was unhandled" message. I guess there should be another argument in the line...
M[i,j]=Double.Parse(split[ i ], SOMETHING);
Any suggestions would be greatly appreciated
Thanks
All replies (8)
Tuesday, September 26, 2006 1:46 PM ✅Answered
Specify System.Globalization.NumberStyles.Float as the second argument and it'll parse most float formats.
Michael Taylor - 9/26/06
Tuesday, September 26, 2006 5:32 PM ✅Answered
Yes the parsing routine will use whatever formatting is specified for your specific region. If you use a , rather than a . then the string would have to be similiar formatted. Now if you know that your numbers will have a decimal rather than a comma then you can programmatically handle that case by doing a search and replace:
stringvalue = stringvalue.Replace('.', ',');
That is probably the easiest to me but if you have other cultural changes that need to be made then you should resort to specifying the NumberFormatInfo to use. This would become the third parameter to the Parse method. Within this type you can specify the formatting of numbers.
string[] values = { "123", "187", "1,10000e+00", "4,00000e+01" };
NumberFormatInfo info = new NumberFormatInfo();
info.NumberDecimalSeparator = ",";
foreach (string value in values)
Console.WriteLine("{0} = {1}", value, Double.Parse(value, NumberStyles.Float, info));
Michael Taylor - 9/26/06
Tuesday, September 26, 2006 2:03 PM
Hmmm... nope... it still doesn't accept the scientific notation.
The first line of the file I'm trying to read is:
123 187 1.10000e+00 4.00000e+01
It accepts the two first numbers (123 and 187)... but refuses the third number ( 1.10000e+00). Is there perhaps some problem with '.' or ',' or the '+' sign?
Thanks for your reply by the way...
Tuesday, September 26, 2006 2:20 PM
I had no problem with the values you gave. Is it possible that you are not parsing the numbers out such that there is garbage on the front or back that is causing problems? Here is the code I used.
string[] values = { "123", "187", "1.10000e+00", "4.00000e+01" };
foreach (string value in values)
{
Console.WriteLine("{0} = {1}", value, Double.Parse(value, System.Globalization.NumberStyles.Float));
};
Michael Taylor - 9/26/06
Tuesday, September 26, 2006 5:22 PM
Hi again...
I still can't make it work. I tried the code snippet you gave me and it doesn't work on my computer. I'm from Norway, could it be that my computer handles '.' differently than yours, that is, my computer expects a ','?
Thanks for all the help by the way. I really appreciate it.
Fettoter
By the way... I tried to isolate each string segment to make sure there was no "garbage" on either side of the string, writing MessageBox.Show("|"+value+"|"), and there was nothing.
Tuesday, September 26, 2006 5:38 PM
Yup!
That did it!!
Thanks a bundle!!
Fettoter
Tuesday, September 26, 2006 7:24 PM
>> Now if you know that your numbers will have a decimal rather than a comma
ahem...<annoying-nit-picking-mode>
Depending on the cultural, it may be a comma or a period (or a full stop), but it is always a decimal point.
</annoying-nit-picking-mode>
Tuesday, September 26, 2006 8:02 PM
Hehe...
I noticed that one too, but I figured I wouldn't enter the annoying-nit-picking-mode since Michael was so helpful and all...
fettoter