在C#中,为什么我会收到“无法将double转换为float错误”的信息? [英] In C#, why am i getting "can't cast double to float error"?

查看:1242
本文介绍了在C#中,为什么我会收到“无法将double转换为float错误”的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行:

    float top = shape.Y + (shape.Height / 2.0) - 4.5;

whic失败,并显示错误消息无法强制翻倍浮动。 Shape.Y和shape.height都是float类型。

whic is failing with the error' Can't cast double to float. Shape.Y and shape.height are both type float.

是什么导致此错误,最好是确保top为float(因为我需要

What is causing this error and what is the best was to make sure top is a float (as i need to pass it into another function tha expects a float.

推荐答案

尝试用 f包裹数字,通常 2.0 代表 double 的数字,您可以阅读有关<$ c $的更多信息c> float 此处

Try to wrap your numbers with f, as normally 2.0 would represent a double number. You can read more about float here.

float top = shape.Y + (shape.Height / 2.0f) - 4.5f;

这篇关于在C#中,为什么我会收到“无法将double转换为float错误”的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆