我有以下代码行:
float top = shape.Y + (shape.Height / 2.0) - 4.5;
失败并出现错误“无法将 double 转换为 float ”。 Shape.Y 和 shape.height 都是 float 类型。
是什么导致了这个错误,最好的办法是确保 top 是一个 float (因为我需要将它传递给另一个需要 float 的函数。
最佳答案
尝试用 f
包裹您的数字,因为通常 2.0
代表一个 double
数字。您可以阅读有关 float
的更多信息 here .
float top = shape.Y + (shape.Height / 2.0f) - 4.5f;
关于c# - 在 C# 中,为什么我会收到 "can' t cast double to float 错误”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318300/