Double.Parse - internalization problem

  • Thread starter Thread starter Adam Klobukowski
  • Start date Start date
A

Adam Klobukowski

Hello

I need to convert some strings int doubles.

Unfortunetly strings do not follow my country culture standarts (we use
, to separate decimal from fraction, but strings use . there etc.).

Also, I have to convert those doubles back to strings, and I need to use
other standars then my culture (ie. use . instead of ,).

The first case (when using standard functions) causes exceptions. The
latter one proble is that ToString() is not good as it follows culture
standards.

How can I convert doubles to strings back and forth using custom culture
standard?
 
Adam Klobukowski said:
I need to convert some strings int doubles.

Unfortunetly strings do not follow my country culture standarts (we use
, to separate decimal from fraction, but strings use . there etc.).

Also, I have to convert those doubles back to strings, and I need to use
other standars then my culture (ie. use . instead of ,).

The first case (when using standard functions) causes exceptions. The
latter one proble is that ToString() is not good as it follows culture
standards.

How can I convert doubles to strings back and forth using custom culture
standard?

Use the version of Double.Parse that takes an IFormatProvider, and pass
in the the appropriate CultureInfo. Similarly give the appropriate
CultureInfo to Double.ToString.
 
You want to use System.Globalization.NumberFormatInfo.
Example:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ",";
string s = "3.1415";
double d = double.Parse(s, nfi);
Console.WriteLine(d);
 
You want the overload of Double.Parse that accepts both a string and an
IFormatProvider.

You can pass in an instance of CultureInfo as the IFormatProvider
implementation. Consider this example:

CultureInfo uk = new CultureInfo("en-GB");
CultureInfo france = new CultureInfo("fr-FR");

string strVal = "123,456";
Console.WriteLine(Double.Parse(strVal));
Console.WriteLine(Double.Parse(strVal, uk));
Console.WriteLine(Double.Parse(strVal, france));


On my machine (which is running in a UK locale), I get:

123456
123456
123.456

as output. So you can see that the one-param version of Double.Parse has
used the local convention for interpretting "," which is that it's a digit
grouping indicator, rather than a decimal point. Passing in an explicit UK
locale has had the same result.

But passing in a French CultureInfo has caused it to treat the "," as a
decimal point.

You could even write your own IFormatProvider implementation, but I would
only do that if there is no culture that meets your requirements.
 
Dennis Myrén napisa³(a):
You want to use System.Globalization.NumberFormatInfo.
Example:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ",";
string s = "3.1415";
double d = double.Parse(s, nfi);
Console.WriteLine(d);

Thanks. That was exactly what I needed.
 
By the way, double to string would be:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ".";

double d = 3.1415;
Console.WriteLine(d.ToString(nfi));
 
Back
Top