Eastbourne Echo
Eastbourne Echo

Reputation: 11

ConvertToDouble when in French locale converting number incorrectly

I have a number stored in the database as a float. In this case the number is 93.75

double localCultreResult;
double.TryParse(rs["CostClaim"].ToString(), System.Globalization.NumberStyles.Any, CultureInfo.CurrentCulture, out localCultreResult);
if (rs["CostClaim"].ToString().Length > 0) {
    Cost.Value = localCultreResult;
};

I want the cost value to display as 93,75 when in French locale but instead the above gives me 9375,00

I have also tried all solutions here: How do I parse a string with a decimal point to a double?

All are converting 93.75 to 9375 when in French locale

Upvotes: 1

Views: 214

Answers (1)

Guillaume Ladeuille
Guillaume Ladeuille

Reputation: 84

If CultureInfo.CurrentCulture is french then rs["CostClaim"].ToString() will be parsed with a ',' as decimal point.

What do you think of:

if (double.TryParse(rs["CostClaim"].ToString(), System.Globalization.NumberStyles.Any, 
    CultureInfo.GetCultureInfo("en-US"), out double localCultreResult))
{
    Cost.Value = localCultreResult;
}

Upvotes: 1

Related Questions