Tuesday, July 17, 2018

c# - Convert Decimal to Double



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.


Answer




An explicit cast to double like this isn't necessary:



double trans = (double) trackBar1.Value / 5000.0;


Identifying the constant as 5000.0 (or as 5000d) is sufficient:



double trans = trackBar1.Value / 5000.0;
double trans = trackBar1.Value / 5000d;


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...