Convert Decimal to Double?

2019-01-04 05:38发布

I want to use a track-bar to change a form's opacity.

This is my code:

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'.

I tried using trans and double but then the control doesn't work. This code worked fine in a past VB.NET project.

13条回答
走好不送
2楼-- · 2019-01-04 06:20

A more generic answer for the generic question "Decimal vs Double?": Decimal for monetary calculations to preserve the precision, Double for scientific calculations that do not get affected by small differences. Since Double is a type which is native to the CPU (internal representation is stored in base 2), calculations made with Double perform better then Decimal (which is represented in base 10 internally).

查看更多
登录 后发表回答