Markus Johansson
Markus Johansson

Reputation: 3773

How to serialize / deserialize a double value independent of culture

How can I serialize / deserialize a double value so that it can be read and written on systems with different decimal point symbols?

Write:

double d;
d.ToString();

Read:

double d = (double)Convert.ChangeType(serialized_value, new Double().GetType());

Upvotes: 3

Views: 1421

Answers (1)

Francis
Francis

Reputation: 3383

You have to specify an invariant format provider

double d = 2.0;

var invariantString = Convert.ToString(d, CultureInfo.InvariantCulture);

var d2 = Convert.ToDouble(invariantString, CultureInfo.InvariantCulture);

Upvotes: 2

Related Questions