Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roundtrip string format for single/double doesn't roundtrip -0 #16783

Closed
mconnew opened this issue Mar 6, 2018 · 0 comments

Comments

@mconnew
Copy link
Member

commented Mar 6, 2018

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 : ∞
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.