方法在文档中的描述不正确 (C#)

huangapple go评论65阅读模式
英文:

Incorrect description of the method in the documentation (С#)

问题

当我使用double.TryParse()方法将字符串转换为浮点数时,该方法返回false,但文档说一切都应该正常工作。

string message = "12.3";
double number;
bool test = double.TryParse(message, out number);

Console.WriteLine(test);

我尝试在方法中使用不同的浮点数据类型,但没有成功。但如果我在字符串中使用,作为小数点分隔符,一切都正常工作,尽管方法文档说得不一样。

string message = "12,3";
double number;
bool test = double.TryParse(message, out number);

Console.WriteLine(test);

我在哪里可以获取关于C#方法如何工作的最新信息?

英文:

When I use the double.TryParse() method to convert a string to a floating point number, the method returns false, but the documentation says everything should work

string message = "12.3";
double number;
bool test = double.TryParse(message, out number);

Console.WriteLine(test);

I tried using different floating point data types in the method, but it didn't work
But if I use "," as a decimal separator in the string everything works, although the method documentation says otherwise

string message = "12,3";
double number;
bool test = double.TryParse(message, out number);

Console.WriteLine(test);

Where can I get up-to-date information on how C# methods work?

答案1

得分: -1

string message = "12.3";
double number;
bool test = double.TryParse(message, NumberStyles.Any, CultureInfo.InvariantCulture, out number);

Console.WriteLine(test); // 这应该打印出 true

英文:
string message = "12.3";
double number;
bool test = double.TryParse(message, NumberStyles.Any, CultureInfo.InvariantCulture, out number);

Console.WriteLine(test); // This should print true

huangapple
  • 本文由 发表于 2023年7月27日 23:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定