请在下面查看我的示例。

float maxFloat = float.MaxValue;
string s = maxFloat.ToString();
float result = float.Parse(s); // same with Convert.ToSingle(s);

bool mustEqual = (maxFloat == result);
// It returns FALSE, why?

最佳答案

您应该使用"R"格式的字符串:

https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#RFormatString


  float maxFloat = float.MaxValue;
  string s = maxFloat.ToString("R"); // <- "R"
  float result = float.Parse(s);

  bool mustEqual = (maxFloat == result);

10-05 23:38