Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Roundtrip string format for single/double doesn't roundtrip -0 #16783
The string format "R" is supposed to allow you to get back the same value as you initially had. When the value is negative zero, the negative gets dropped and the value does not round trip. This can be easily seen when dividing 1 by the value as you will get either positive or negative infinity.
double initialValue = -0.0; string valueString = initialValue.ToString("R", CultureInfo.InvariantCulture); double roundTripped = double.Parse(valueString, CultureInfo.InvariantCulture); Console.WriteLine("1 / initialValue : " + 1 / initialValue); Console.WriteLine("1 / roundTripped : " + 1 / roundTripped); // Output: // 1 / initialValue : -∞ // 1 / roundTripped : ∞