Parsing an english-style double on an italian Windows system

  • Thread starter Thread starter Massimo
  • Start date Start date
M

Massimo

As the title implies, I'm in need of reading a double number from a text
file, but the file follows english-style syntax, while the application is
running on an italian-language Windows system; this causes some troubles,
because in Italy we use "." to separate thousands and "," for decimal
digits, while the english number format does exactly the opposite.

I know there must be some way to tell double.Parse() to use a different
number format, but I really don't know how to do this; I suppose
double.Parse(String,IFormatProvider) can do the trick, but I don't seem to
be able to find any information about how to use it.

Can someone please help?

Thanks


Massimo
 
Thus wrote Massimo,
As the title implies, I'm in need of reading a double number from a
text file, but the file follows english-style syntax, while the
application is running on an italian-language Windows system; this
causes some troubles, because in Italy we use "." to separate
thousands and "," for decimal digits, while the english number format
does exactly the opposite.

I know there must be some way to tell double.Parse() to use a
different number format, but I really don't know how to do this; I
suppose double.Parse(String,IFormatProvider) can do the trick, but I
don't seem to be able to find any information about how to use it.

Can someone please help?

Create a CultureInfo for "en-US" (or "en-GB") and pass this as IFormatProvider:

// Prints "10000"
Console.WriteLine(Double.Parse("10,000", new CultureInfo("en-US")));

Cheers,
 
Create a CultureInfo for "en-US" (or "en-GB") and pass this as
IFormatProvider:

// Prints "10000"
Console.WriteLine(Double.Parse("10,000", new CultureInfo("en-US")));

Thanks.

I know it should have been quite simple, but MSDN docs are so huge sometimes
it's difficult to find what you need.


Massimo
 
Back
Top