6

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.

8
  • What do you want it to be parsed as? Commented May 29, 2011 at 23:52
  • Who's giving you such odd data? It seems the real problem is with the source... Commented May 29, 2011 at 23:54
  • @soandos I want it to be parsed as a float number. Originally it is string. Commented May 29, 2011 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... en.wikipedia.org/wiki/Decimal_mark Commented May 29, 2011 at 23:56
  • @Cameron Maybe but I can't change the source Commented May 29, 2011 at 23:56

3 Answers 3

12

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);
Sign up to request clarification or add additional context in comments.

4 Comments

The one risk with this approach would be if numbers could ever be in the format of 1,000.00
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.
That's not an option. I cannot change anything of the source.
I 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.
4

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).

Comments

2

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.

2 Comments

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?
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 :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.