By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

Double.Parse - internalization problem

P: n/a
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?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/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);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Adam Klobukowski" <at***@gabo.pl> wrote in message
news:cn**********@atlantis.news.tpi.pl...
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?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Nov 16 '05 #3

P: n/a
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?

Nov 16 '05 #4

P: n/a
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.

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl
Nov 16 '05 #5

P: n/a
By the way, double to string would be:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ".";

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

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Adam Klobukowski" <at***@gabo.pl> wrote in message
news:cn**********@atlantis.news.tpi.pl...
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.

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.