Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double parse with culture format

Tags:

c#

int32

I have a double number as string. The number is

202.667,40

Which is 202667.4

How can I parse this string to get the value like: Double.Parse("202.667,40",?what here), or any other method to get the value would be great. Thanks

like image 291
Ryan Avatar asked Feb 24 '11 20:02

Ryan


People also ask

What does double parse mean?

Description. Double Parse(String, NumberStyles, IFormatProvider) converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent.

How do I convert a string to a double in C#?

String value can be converted to double using Convert. ToDouble() or Double. Parse() method. These methods take string representation of a number as input and return its equivalent double-precision floating-point number.

How do you convert a string to a double in flutter?

For converting a String to a double, the parse() static method of double class can be used. String source, [@deprecated double onError(String source)?] );


4 Answers

First, you need to know which culture this number is from, then:

CultureInfo culture = new CultureInfo("de"); // I'm assuming german here.
double number = Double.Parse("202.667,40", culture);

If you want to parse using the current thread culture, which by default is the one set for the current user:

double number = Double.Parse("202.667,40", CultureInfo.CurrentCulture);
like image 119
Coincoin Avatar answered Sep 22 '22 23:09

Coincoin


I think i have found a solution which does not require a culture. Using a NumberFormatInfo you can force a format, no matter the culture:

// This is invariant
NumberFormatInfo format = new NumberFormatInfo();
// Set the 'splitter' for thousands
format.NumberGroupSeparator = ".";
// Set the decimal seperator
format.NumberDecimalSeparator = ",";

Then later:

System.Diagnostics.Debug.WriteLine(double.Parse("202.667,40", format)));

Outputs:
202667,4

Of course, this output (inner toString()) might differ per Culture(!)
Note that changing the input to "202,667.40" will result in a parse error, so the format should match your expected input.

Hope this helps someone..

like image 31
patman77 Avatar answered Sep 23 '22 23:09

patman77


Instead of having to specify a locale in all parses, I prefer to set an application wide locale, although if string formats are not consistent across the app, this might not work.

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-PT");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("pt-PT");

Defining this at the begining of your application will make all double parses expect a comma as the decimal delimiter.

like image 29
Miguel Mesquita Alfaiate Avatar answered Sep 22 '22 23:09

Miguel Mesquita Alfaiate


You could use Double.Parse(your_number, CultureInfo.CurrentCulture) and set CurrentCulture accordingly with Thread.CurrentThread.CurrentCulture.

Example:

Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");

then later

Double.Parse(your_number, CultureInfo.CurrentCulture);

Note that if you explicitly assign the culture to the CurrentThread, it only applies to that thread.

like image 27
Holstebroe Avatar answered Sep 25 '22 23:09

Holstebroe