c# - 为什么在尝试将 double 转换为 float 时出现错误?

标签 c#

我在将 double 转换为 float 时遇到一点问题。代码:

float volume = 0.5;
Double i = Volume.Value;
volume = (float)i / 100F;
Bass.BASS_SetVolume(volume);

如您所见,我正在使用 BASS 库。音量是一个 slider ,它给我一个从 1 到 100 的值。问题是,我收到了这个错误:

error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

为什么会出现此错误?

最佳答案

float volume = 0.5;

这一行会给你你的错误。默认情况下, float 是 double,因此它不能将 0.5 隐式转换为 float 。尝试改用 0.5f

关于c# - 为什么在尝试将 double 转换为 float 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18861658/

相关文章:

c# - 覆盖 CompareTo : What to do with null case?

c# - 为什么我在 DataSet 上收到 NullReferenceException?

c# - 如何从 IIS 通过 C# 通过网站名称获取端口?

c# - 为什么我应该使用 SecureString 而不是字符串?

c# - 使用 Linq 和 SQL 时如何防止将新记录插入表中?

c# - 在创建容器时将无参数构造函数设置为注入(inject)构造函数

c# - 事件源未记录

c# - 如何在 C# 中检测 Windows Server 2008 上的防病毒软件?

c# - 错误消息 “An object reference is required for the non-static field, method or property.”

c# - 删除自定义 HashSet