views:

65

answers:

4

Hi,

I have written a small program where the program works differently on different operating systems (xp, win7) The problem is the program reads some float numbers such 2,686.

One operating system (win7) convert it to float true, but on xp it goes wrong and print it 2686. How can I understand which symbol the operation system uses for decimal numbers ?

Thanks.

+4  A: 
string sep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
LukeH
I think the question is about decimal separator, not about grouping.
Henk Holterman
@Henk: You're right. In that case it'll be `NumberDecimalSeparator` instead. Updated.
LukeH
thanks for the answer and thansk for all, the answers were correct.
Cmptrb
+2  A: 

This does not depend on the operating system but at the (default) language settings on each PC.

If you use : double value = double.Parse(text); you are using whatever culture the user has configured. If you know the input to be in a certain format, use:

var ci = CultureInfo.GetCulture("nl-NL");  // dutch
double value = double.Parse(text, ci);

Every function that converts has (1 or more) overloads to take a FormattingProvider (Culture).

Henk Holterman
+2  A: 

parse the floating point numbers using the user current culture with double.Parse(string, System.Globalization.CultureInfo.CurrentCulture);

munissor
+1  A: 

The decimal separator is decided by the current culture.

If you want to use a specific character as decimal separator, you can create a custom NumberFormatInfo object with any separator you like. If you want to use period as deimcal separator, you can simply use InvariantCulture:

double n = Double.Parse(s, CultureInfo.InvariantCulture);

If you want to use comma, you can choose a culture that has that, for example swedish:

double n = Double.Parse(s, CultureInfo.GetCultureInfo("sv-SE"));
Guffa