I'm reading numbers from XML files. Other numbers are with a comma separator (0,1111) and others with dot (0.1111). How do I parse these numbers so I get the desired result in the end? I tried using float.Parse(reader.Value, System.Globalization.CultureInfo.InvariantCulture);
but it doesn't work. For example I have reader.Value = "0,01119703" and is parsed as 1119703.0.

- 3,291
- 4
- 35
- 48

- 2,159
- 6
- 30
- 40
-
What do you want it to be parsed as? – soandos May 29 '11 at 23:52
-
Who's giving you such odd data? It seems the real problem is with the source... – Cameron May 29 '11 at 23:54
-
@soandos I want it to be parsed as a float number. Originally it is string. – user579674 May 29 '11 at 23:56
-
@Cameron, not all locales use . as their decimal separator, thus if this were an application where you may be receiving numbers from en-US and fr-CA for instance, you may get . or , respectively. For more info... http://en.wikipedia.org/wiki/Decimal_mark – Chris Baxter May 29 '11 at 23:56
-
@Cameron Maybe but I can't change the source – user579674 May 29 '11 at 23:56
-
@user - do you have any way to determine the correct locale for the number? or is everything expected to be in a specific culture? – Chris Baxter May 29 '11 at 23:57
-
@Calgary: Thanks, but I knew that -- I meant that the data should be consistent (either only one culture, or have the culture specified with each string, etc.). A mishmash of differently formatting numbers isn't very machine-friendly – Cameron May 29 '11 at 23:59
-
@Calgary No I guess. If so I could parse it easily. Assume that I have a bunch of strings of which some are x,xxx and some x.xxx – user579674 May 30 '11 at 00:01
3 Answers
I don't believe that it is possible to work with two different decimal separators at the same time. I think I would just use Replace() to change any commas into dots.
float.Parse(reader.Value.Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);

- 5,145
- 34
- 37
-
The one risk with this approach would be if numbers could ever be in the format of 1,000.00 – Chris Baxter May 29 '11 at 23:57
-
That's a little dodgy. If I enter "3,456.78", the code above will change it to "3.456.78", which will probably result in a parse error. – Samir Talwar May 29 '11 at 23:57
-
-
2I am not talking about changing the xml file, just changing the code, so that you replace any commas with dots before you pass them into float.Parse(). As others have pointed out, this will break if there can be thousands separators. In that case you would probably have to use a regex. – AHM May 30 '11 at 00:01
Not sure this is the greatest solution, but perhaps you could rely on a set of known "Custom" number formats. For instance, you could declare two custom number formats (either from scratch or based off of a known format) such as:
private static readonly NumberFormatInfo DecimalSeparatorFormat = new NumberFormatInfo { NumberDecimalSeparator = ".", NumberGroupSeparator = "," };
private static readonly NumberFormatInfo CommaSeparatorFormat = new NumberFormatInfo { NumberDecimalSeparator = ",", NumberGroupSeparator = "." };
And then try parsing the number through your known accepted formats:
if (!Single.TryParse(unparsedValue, NumberStyles.Float, DecimalSeparatorFormat, out parsedValue) && !Single.TryParse(unparsedValue, NumberStyles.Float, CommaSeparatorFormat, out parsedValue))
throw new FormatException("Number format not supported");
This assumes that you have a finite number of known formats, if your inputs can truly be in any culture, then you may be out of luck with finding a great solution.
The one win with this approach is you are at least being explicit in the formats you are able to support rather than relying on a simple string replace (which may result in an invalid format).

- 16,083
- 9
- 51
- 72
Is there anything in the XML files that will tell you which format is being used? There's not a built-in way in .NET to have two different allowed decimal separators. If there's nothing telling you which format a number is going to be in, then you could always check to see whether the string contains a period or a comma, and create a NumberFormatInfo with that as the decimal separator. Of course, this won't work if any of the numbers have a period or comma as a thousands separator.

- 16,674
- 4
- 44
- 56
-
They are small numbers so probably there isn't any thousand separator. How do I check if it is a comma or dot? Do I have to check the string if it contains those characters? – user579674 May 29 '11 at 23:59
-
You don't have to check anything, just use the Replace() function. If it doesn't find the character to replace, it returns the original string. See the code I posted in my answer :-) – AHM May 30 '11 at 00:03