0

I am currently trying to parse a string, "277.968", to decimal, but I am getting a FormatException exception.

I have read that I need to perform the decimal parse this way:

string str = "277.968";
decimal.Parse(str, CultureInfo.InvariantCulture);

Still, I am getting the said exception.

What could I do?

EDIT: Fixed float to decimal

Guido Magrin
  • 551
  • 3
  • 7
  • 20
  • 4
    Is that your *exact* code, or are you actually parsing a string from elsewhere? I wonder whether there's some invisible character in the value, for example. – Jon Skeet Mar 01 '14 at 14:36
  • 1
    My exact code, at the moment, is the following: Debug.WriteLine(new string(risul)); Debug.WriteLine(decimal.Parse(new string(risul), CultureInfo.InvariantCulture)); While the 1st line prints correctly "277.968", the second throws the exception – Guido Magrin Mar 01 '14 at 14:38
  • Why are you using `new string(risul)`? What is the type of `risul`? Can you print the *length* of the string? Where is the data coming from? Note that you've specified `float.Parse` in the question, but `decimal.Parse` in the comment - which is it? What happens if you try `decimal.Parse("277.968", CultureInfo.InvariantCulture)`? – Jon Skeet Mar 01 '14 at 14:39
  • risul is actually a char array. Printing the lenght of the string, it reported it being 80 chars long. Fixed the decimal/float part, it was just a typing mistake – Guido Magrin Mar 01 '14 at 14:43
  • I checked string length this way: Debug.WriteLine("Length: " + new string(risul).Length); – Guido Magrin Mar 01 '14 at 14:44

1 Answers1

3

Printing the lenght of the string, it reported it being 80 chars long.

Right, well that's the problem then. Your string isn't "277.968" - it's "277.968\0\0\0\0\0\0(...)" - and that can't be parsed.

My guess is that you've read this from a TextReader of some kind, but ignored the return value of Read, which is the number of characters that have been read.

So for example, if your current code is effectively:

char[] risul = new char[80];
reader.Read(risul, 0, risul.Length);
decimal value = decimal.Parse(new string(risul), CultureInfo.InvariantCulture);

then you should instead have:

char[] risul = new char[80];
int charsRead = reader.Read(risul, 0, risul.Length);
decimal value = decimal.Parse(new string(risul, 0, charsRead),
                              CultureInfo.InvariantCulture);

... although that's still assuming that you're reading all of the appropriate data in a single call to Read, which isn't necessarily the case. You may well just want:

string data = reader.ReadToEnd();
decimal value = decimal.Parse(data, CultureInfo.InvariantCulture);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That looks right, still I can't get it working. I even tried forcing the charsRead value to 4, so I can be sure that, in this case, the read characters are actually even less than the ones I need, but I still get the FormatException. Debug.WriteLine(decimal.Parse(new string(risul, 0, 4), CultureInfo.InvariantCulture)); Throws it – Guido Magrin Mar 01 '14 at 14:54
  • @guido1993: Well have you tried the code I previously suggested, just hard-coding `decimal.Parse("277.698")`? Is it possible that actually it's the *start* of the string that contains unprintable characters? You should separate the problems of "getting the right string value" and "parsing the string value". Currently we're diagnose both at the same time, which is not productive. – Jon Skeet Mar 01 '14 at 14:58
  • I just tried printing this: Debug.WriteLine(decimal.Parse("277.698")); I am getting (around, didn't feel like counting them) 20 lines of 277698. I feel more and more confused. I think it's not possible for the header of the string to contain these said unprintable characters – Guido Magrin Mar 01 '14 at 15:02
  • I tried decimal.Parse("277.698",CultureInfo.InvariantCulture), it will print, still like 20 times, 277.698. Still confused about why it should print it 20 times – Guido Magrin Mar 01 '14 at 15:50
  • @guido1993: Okay, so it sounds like you've got it in a loop. However, you haven't shown us any of that code. It definitely sounds like the problem is with how you're getting the data, not with the parsing itself. – Jon Skeet Mar 01 '14 at 16:35