Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未将double.MaxValue转换回字符串_C#_.net - Fatal编程技术网

C# 未将double.MaxValue转换回字符串

C# 未将double.MaxValue转换回字符串,c#,.net,C#,.net,我有一个简单的问题这个测试怎么会失败?注释中是变量的值 [Fact] public void ToNullableDouble_CorrectInput_ReturnsCorrectValue() { double expectedValue = double.MaxValue; //1.7976931348623157E+308 string expectedValueString = expectedValue.ToString(); //"1,79769313486232E

我有一个简单的问题这个测试怎么会失败?注释中是变量的值

[Fact]
public void ToNullableDouble_CorrectInput_ReturnsCorrectValue()
{
    double expectedValue = double.MaxValue; //1.7976931348623157E+308
    string expectedValueString = expectedValue.ToString(); //"1,79769313486232E+308" - fail, I do not know why
    string expectedValueString2 = expectedValue.ToString(CultureInfo.InvariantCulture); //"1.79769313486232E+308" - fail
    string expectedValueString3 = 1.75.ToString(); //"1,75" - pass
    string expectedValueString4 = 1.75.ToString(CultureInfo.InvariantCulture); //"1.75" - fail because of the culture

    double number;
    double? convertedValue = double.TryParse(expectedValueString, out number) ? number : (double?)null;
    //convertedValue = null, number = 0

    Assert.True(convertedValue.HasValue);
}
Float的行为非常相似,十进制也可以。我知道小数类型之间的区别。但我不明白MaxValue常量怎么能不转换。不应该是区域性设置:

double.TryParse(expectedValueString, out number) ? number : (double?)null;
null
double.TryParse(expectedValueString2, out number) ? number : (double?)null;
null
double.TryParse(expectedValueString3, out number) ? number : (double?)null;
1.75
double.TryParse(expectedValueString4, out number) ? number : (double?)null;
null

对于其他值,例如:1.75

此问题可能是由于double.MaxValue已转换为字符串。字符串输出不包含所有数字,而是四舍五入。解析此值会使double溢出

解决方案1:

string stringDouble = double.MaxValue.ToString("r");
double doubleValue = double.Parse(stringDouble); // No exception
string stringDouble = double.MaxValue.ToString();
double doubleValue;
if (!double.TryParse(stringDouble, out doubleValue)) 
{
    doubleValue = stringDouble.Equals(double.MaxValue) ? double.MaxValue : double.MinValue;
}

解决方案2:

string stringDouble = double.MaxValue.ToString("r");
double doubleValue = double.Parse(stringDouble); // No exception
string stringDouble = double.MaxValue.ToString();
double doubleValue;
if (!double.TryParse(stringDouble, out doubleValue)) 
{
    doubleValue = stringDouble.Equals(double.MaxValue) ? double.MaxValue : double.MinValue;
}

失败的是什么?convertedValue为null,应该是double。MaxValueCheck下面的答案解释了原因。