Problems with Double formatting

Asked

Viewed 905 times

6

I have a method that gets values like string and performs the conversion to other types, my problem is that when converting an information to double It changes its precise value so that it doesn’t happen. For example, I get "19.30". use the method below to convert and end up with the value "1930,00", or something similar.

I get "19.30" in variable x.

val = Convert.ToDouble(x);

The value of val is for example: "19300,0" but I need the value to be the same as in string: "19.30", for I will use it to do various calculations with it. If you can convert to currency also serves as long as the format is the same as I receive as string

Is there any method to make this conversion without changing the value?

3 answers

6

6


You probably have a "culture" problem. The . NET takes the information about the culture being used on the computer. It "reads the environment" and does operations according to the information provided by the operating system. One of the ways to achieve the conversion is to have it executed without considering the specific culture of the environment. That is, to make culture invariant.

You can try set an invariant culture for the whole thread:

Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

Documentation of Thread.CurrentThread.CurrentCulture

Or you can set the culture into one Overload of a method that does the conversion and accepts this setting:

double.TryParse(valor, NumberStyles.Any, CultureInfo.InvariantCulture, out valorDouble)

Documentation of double.TryParse()

or

Convert.ToDouble(valor, CultureInfo.InvariantCulture)

Documentation of Convert.ToDouble()

I put in the Github for future reference.

In the latter examples I am considering that you are using a using System.Globalization to access the class CultureInfo.

Surely there are other ways to do it and some may meet different situations. You need to understand the subject well. It’s not as simple as it seems I’ve shown you where you can start.

Of course you can use a specific culture if this is necessary. See the whole class CultureInfo.

  • OK thanks for the answers but I will use this one because it is very easy to use. Convert.Todouble(value, Cultureinfo.Invariantculture)

  • I believe for your case it is. I’ll even improve some things (after the game :) )

4

Thread.Currentculture

As the staff said, the parse method will use the current thread culture to interpret/format the value.

Changing the current thread culture is just a matter of changing the property Currentculture of the current thread:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; // ou a cultura que desejar

CultureInfo.DefaultThreadCurrentCulture

From . NET 4.5 you can set a default culture for all threads for your application (for your application Domain to be more specific. It can be very useful if your application uses/creates multiple threads.

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("pt-BR");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.