Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# parsing float from string

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.

like image 731
user579674 Avatar asked May 29 '11 23:05

user579674


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


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);
like image 75
AHM Avatar answered Oct 17 '22 20:10

AHM


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

like image 41
Chris Baxter Avatar answered Oct 17 '22 19:10

Chris Baxter


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.

like image 21
Daniel Plaisted Avatar answered Oct 17 '22 20:10

Daniel Plaisted