Header add

In this article I will describe How to convert a Decimal to a Double in C#?

Recently I have faced a issue using the decimal value. 

  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

Solution :

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; 

Post a Comment

Previous Post Next Post