Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to parse float?

What is the best way to parse a float in CSharp? I know about TryParse, but what I'm particularly wondering about is dots, commas etc.

I'm having problems with my website. On my dev server, the ',' is for decimals, the '.' for separator. On the prod server though, it is the other way round. How can I best capture this?

like image 675
Boris Callens Avatar asked Sep 29 '08 07:09

Boris Callens


People also ask

How do you parse a float?

The parseFloat() function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.

Does parseInt work on float?

parseInt is for converting a non integer number to an int and parseFloat is for converting a non float (with out a decimal) to a float (with a decimal). If your were to get input from a user and it comes in as a string you can use the parse method to convert it to a number that you can perform calculations on.

How do you keep a float up to 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

What does parseFloat return?

The parseFloat() function parses an argument (converting it to a string first if needed) and returns a floating point number.

How do I parse a float file?

For our float parser, that type would be Float. There are basically two functions to execute parsers: parse and parseFromFile. This big type signature is actually fairly simple: it takes a parser, a source name (a String used as a part of error messages), a Stream, and either results in a ParseError or the result type of the parser.

Is it better to parse or parse a string?

if the strings you parse are mostly valid floats, tryparse is definitely faster. if you have lots of different strings maybe parse is the better choice, but when getting lots of strings (dozens of thousands) that are all (in 99% of the cases) valid floats in string representation, use tryparse. Are you sure you don't mean the other way round?

How do you make a complex parser?

Through libraries such as Parsec it's possible to build up complex parsers by using simple parsers and parser combinators. Not only does this result in code that is short and easy to understand, it provides good performance and helpful error messages for free. To start things simple, we'll create a parser for integers.

How do I parse a string in Python?

If your input comes from an internal source, you can use the InvariantCulture to parse the string. The Parse method is somewhat easier to use, if your input is from a controlled source. That is, you have already validated the string. Parse throws a (slow) exception if its fails.


4 Answers

I agree with leppie's reply; to put that in terms of code:

string s = "123,456.789";
float f = float.Parse(s, CultureInfo.InvariantCulture);
like image 101
Marc Gravell Avatar answered Oct 05 '22 23:10

Marc Gravell


Depends where the input is coming from.

If your input comes from the user, you should use the CultureInfo the user/page is using (Thread.CurrentThread.CurrentUICulture).

You can get and indication of the culture of the user, by looking at the HttpRequest.UserLanguages property. (Not correct 100%, but I've found it a very good first guess) With that information, you can set the Thread.CurrentThread.CurrentUICulture at the start of the page.

If your input comes from an internal source, you can use the InvariantCulture to parse the string.

The Parse method is somewhat easier to use, if your input is from a controlled source. That is, you have already validated the string. Parse throws a (slow) exception if its fails.

If the input is uncontrolled, (from the user, or other Internet source) the TryParse looks better to me.

like image 38
GvS Avatar answered Oct 05 '22 23:10

GvS


If you want persist values ( numbers, date, time, etc... ) for internal purpose. Everytime use "InvariantCulture" for formating & parsing values. "InvariantCulture" is same on every computer, every OS with any user's culture/language/etc...

string strFloat = (15.789f).ToString(System.Globalization.CultureInfo.InvariantInfo);
float numFloat  = float.Parse(System.Globalization.CultureInfo.InvariantInfo, strFloat);
string strNow   = DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantInfo);
DateTime now    = DateTime.Parse(System.Globalization.CultureInfo.InvariantInfo, strNow);
like image 7
TcKs Avatar answered Oct 06 '22 00:10

TcKs


You could always use the overload of Parse which includes the culture to use?

For instance:

double number = Double.Parse("42,22", new CultureInfo("nl-NL").NumberFormat); // dutch number formatting

If you have control over all your data, you should use "CultureInfo.InvariantCulture" in all of your code.

like image 6
Davy Landman Avatar answered Oct 05 '22 23:10

Davy Landman