Parse String to Enum Type

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

Double.Parse using specific culture

add this:
ci.NumberFormat.NumberGroupSeparator = ".";

CultureInfo and Double.parse and double.toString not worked correctly

I defined a method and use it in every where in application that changes cultureinfo

public static void CorrectNumberFormat()
{
var culture = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
if (culture.NumberFormat.NumberDecimalSeparator != ".")
{
culture.NumberFormat.NumberDecimalSeparator = ".";
culture.NumberFormat.CurrencyDecimalSeparator = ".";
culture.NumberFormat.PercentDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}

how to handle culture in double parsing in c#?

No, there is no generic way. You either need to know what culture the double was formatted or all the servers need to send in a single format, say InvariantCulture.

If you guess the culture, there's a bad news waiting for you as different cultures use different decimal separator, group separator etc. So you can't.

Refer this to foresee what can go wrong if you guess.

What is the best way to parse double from unknown culture in .NET C#?

Honestly I don't think your current solution is too bad. It isn't elegant, but you are given non-elegant data. As others have suggested, I would see if it is possible to get the XML files in a consistent format, or at least have the XML files saved with a culture info:

<yourRootElement xml:lang="en-US">

That way you won't have to guess.

Barring that, you could also do something like this:

private double StringToDouble(string input)
{
var last = input.LastIndexOfAny(new[] {',', '.'});
var separator = last >= 0 ? input[last] : '.';
var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = separator.ToString(CultureInfo.InvariantCulture);
return double.Parse(input, clone);
}

CultureInfo.Clone is expensive, but you can cache culture info based on what the separator is. This also gives you the flexibility to set up different thousands separators, if needed. You would have to assume what the thousands separator is depending on the decimal separator.

How to parse double string with comma?

You should specify expected culture format, however you can just specify number separator like that:

Double.Parse(
"-0,233",
new CultureInfo(CultureInfo.CurrentCulture.Name)
{
NumberFormat = new NumberFormatInfo() { NumberDecimalSeparator = ","}
}
)

Parse strings to double with comma and point

You want to treat dot (.) like comma (,). So, replace

if (double.TryParse(values[i, j], out tmp))

with

if (double.TryParse(values[i, j].Replace('.', ','), out tmp))

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

How do I parse string with decimal separator to a double c#?

In addition to replace comma with dot, you need to supply a proper number format:

public double ParseMyString(string myString)
{
return double.Parse(myString.Replace(',', '.'),
new NumberFormatInfo() {NumberDecimalSeparator = "."});
}

Another option to replace the separator on a broader scope is to use this:

Thread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

You will still need to replace comma with dot though.

Double.Parse - Internationalization problem

It is taking the culture you gave and applying the correct formatting. You provided a string of "0.009" and told it that it was Spanish...then you complain that it properly interpreted it as Spanish! Don't tell it that the string is Spanish when you know it isn't.

You should pass the Parse method the culture of the string being parsed, which in this case would be en-US or en-Gb or InvariantCulture.



Related Topics



Leave a reply



Submit