How to Convert String to Double with Proper Cultureinfo

How to convert string to double with proper cultureinfo

You need to define a single locale that you will use for the data stored in the database, the invariant culture is there for exactly this purpose.

When you display convert to the native type and then format for the user's culture.

E.g. to display:

string fromDb = "123.56";
string display = double.Parse(fromDb, CultureInfo.InvariantCulture).ToString(userCulture);

to store:

string fromUser = "132,56";
double value;
// Probably want to use a more specific NumberStyles selection here.
if (!double.TryParse(fromUser, NumberStyles.Any, userCulture, out value)) {
// Error...
}
string forDB = value.ToString(CultureInfo.InvariantCulture);

PS. It, almost, goes without saying that using a column with a datatype that matches the data would be even better (but sometimes legacy applies).

Convert.ToDouble for different CultureInfo values

Before using CurrentCulture you need to set it as you know the culture like
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

Globalization .net convert string to double

Yes, it's your current culture that converts it this way. You can use CultureInfo.InvariantCulture to skip using your culture.

double d = double.Parse("65.50", CultureInfo.InvariantCulture);

i want it to be 65.50.

If you want to convert it back to string:

string str = d.ToString("N2", CultureInfo.InvariantCulture);  

I assume this is a currency since you keep the decimal places. Then you should use decimal instead:

decimal dec = decimal.Parse("65.50", CultureInfo.InvariantCulture); // 65.5

Now you can use decimal.ToString and it automagically restores the decimal places:

string str = dec.ToString(CultureInfo.InvariantCulture); // "65.50"

Double parse with culture format

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

C# convert string to double in locale

You need to specify the culture as the second argument to double.Parse, e.g.

double da = double.Parse(a, CultureInfo.InvariantCulture);

Pretty much all of the formatting/parsing methods have overloads taking an IFormatProvider, and the most commonly-specified implementation of IFormatProvider is CultureInfo.

Converting string to double in C#

There are 3 problems.

1) Incorrect decimal separator

Different cultures use different decimal separators (namely , and .).

If you replace . with , it should work as expected:

Console.WriteLine(Convert.ToDouble("52,8725945"));

You can parse your doubles using overloaded method which takes culture as a second parameter. In this case you can use InvariantCulture (What is the invariant culture) e.g. using double.Parse:

double.Parse("52.8725945", System.Globalization.CultureInfo.InvariantCulture);

You should also take a look at double.TryParse, you can use it with many options and it is especially useful to check wheter or not your string is a valid double.

2) You have an incorrect double

One of your values is incorrect, because it contains two dots:

15.5859949000000662452.23862099999999

3) Your array has an empty value at the end, which is an incorrect double

You can use overloaded Split which removes empty values:

string[] someArray = a.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);

how to change culture info on the string or double

This is not related to Culture info.

Looks like you are getting a measurement in feet while you are expecting it to be in meters. In fact, 100 meters = 328.08399 feet and your measurements might be in 10 feets i.e 3015.0 = 301.5 feet (some GPS receivers do not support floats or doubles and therefore return only integers multiplied by 10 to have one decimal accuracy)

If you are using a cheap GPS receiver than this is expected as the accuracy is not that great (this would explain why you are getting 3015.0 instead of 3280)

I hope this helps.

.NET Double.TryParse(num, format, cultureinfo, out) Error

@tarekgh posted an answer on the GitHub issue. The following is what he wrote:

"The problem here is the failing cultures for you have the following:

Decimal separator is ","
Group Separator is "."
Currency Decimal separator is "."
Currency Group Separator is ","
Notice that the decimal separators is same as the currency group separator. Also the group separator is same as the currency decimal separator.

Now when you format the number with such culture, you'll get the string "15.000,05". when you are trying to parse it back you are passing NumberStyles.Any which means the string can be a currency number or it can be decimal number. This confuse the parser when trying to parse the character "." as it can be treated as currency decimal separator or it can be treated as group separator. The parser decide to treat it as currency decimal separator. Then the parser will continue till hitting "," and again will treat this as currency group separator. because the group separator cannot come after the decimal separator, the parser will fail to parse the string and will return false from TryParse (or throw exception from Parse).

The way to fix this, remove the currency parsing from passed NumberStyles. i.e.

        Double.TryParse(numString, NumberStyles.Any & (~NumberStyles.AllowCurrencySymbol), ci, out numParsed);

I am going to close the issue but feel free to respond back with any more questions if you have any."

Convert String to Double, Explicitly Using Period as Decimal Point

Use CultureInfo.InvariantCulture while parsing.

double d = double.Parse("3.14", CultureInfo.InvariantCulture);

See: CultureInfo.InvariantCulture Property

The invariant culture is culture-insensitive; it is associated with
the English language but not with any country/region.



Related Topics



Leave a reply



Submit