-1
我有簡單的問題。 這個測試如何失敗?在評論中是變量的值double.MaxValue to string is unsverted back
[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);
}
浮動行爲非常相似,小數是好的。我知道小數類型有什麼區別。但我不明白如何可以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
什麼是失敗? – mybirthname
convertedValue爲null,應該是double.MaxValue – Marv
檢查下面的答案是解釋原因。 – mybirthname