如何在C#中将小数转换为双精度?
问题描述:
我想使用轨迹栏
更改 Form
的不透明度.
I want to use a Track-Bar
to change a Form
's opacity.
这是我的代码:
decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
构建应用程序时,它出现以下错误:
When I build the application, it gives the following error:
Cannot implicitly convert type decimal to double
我尝试使用 trans
和 double
,但是 Control
不起作用.这段代码在过去的VB.NET项目中运行良好.
I have tried using trans
and double
, but then the Control
doesn't work. This code worked fine in a past VB.NET project.
答
不需要像这样显式转换为 double
:
An explicit cast to double
like this isn't necessary:
double trans = (double) trackBar1.Value / 5000.0;
将常量标识为 5000.0
(或 5000d
)就足够了:
Identifying the constant as 5000.0
(or as 5000d
) is sufficient:
double trans = trackBar1.Value / 5000.0;
double trans = trackBar1.Value / 5000d;