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.
--
Ian Griffiths -
http://www.interact-sw.co.uk/iangblog/
DevelopMentor -
http://www.develop.com/
"Adam Klobukowski" <at***@gabo.pl> wrote:
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?