2

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());
Markus Johansson
  • 3,733
  • 8
  • 36
  • 55

1 Answers1

2

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);
Francis
  • 3,335
  • 20
  • 46
  • that does not work for RU-ru culture. example: var str = "0,6"; var d2 = Convert.ToDouble(str, CultureInfo.InvariantCulture); // here d2 is "6.0" not "0.6" – Alexeyss May 04 '12 at 08:00