When setting a form's opacity should I use a decimal or 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 try to build it, I get this error:

Cannot implicitly convert type ‘decimal’ to ‘double’.

I tried making trans a double, but then the control doesn’t work. This code has worked fine for me in VB.NET in the past.

Add Comment
13 Answer(s)

Assuming you are using WinForms, Form.Opacity is of type double, so you should use:

double trans = trackBar1.Value / 5000.0;
this.Opacity = trans;

Unless you need the value elsewhere, it’s simpler to write:

this.Opacity = trackBar1.Value / 5000.0;

The reason the control doesn’t work when you changed your code to simply be a double was because you had:

double trans = trackbar1.Value / 5000;

which interpreted the 5000 as an integer, so your trans value was always zero. By explicitly making the numeric a floating point value by adding the .0 the compiler can now interpret it as a double and perform the proper calculation.

Answered on July 15, 2016.
Add Comment

The best solution is:

this.Opacity = decimal.ToDouble(trackBar1.Value/5000);
Answered on July 15, 2016.
Add Comment

An explicit cast to double 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;
Answered on July 15, 2016.
Add Comment

Your code worked fine in VB.NET because it implicitly does any casts, while C# has both implicit and explicit ones.

In C# the conversion from decimal to double is explicit as you lose accuracy. For instance 1.1 can’t be accurately expressed as a double, but can as a decimal (see “Floating point numbers – more inaccurate than you think” for the reason why).

In VB the conversion was added for you by the compiler:

decimal trans = trackBar1.Value / 5000m;
this.Opacity = (double) trans;

That (double) has to be explicitly stated in C#, but can be implied by VB’s more ‘forgiving’ compiler.

Answered on July 15, 2016.
Add Comment

In my opinion, it is desirable to be as explicit as possible. This adds clarity to the code and aids your fellow programmers who may eventually read it.

In addition to (or instead of) appending a .0 to the number, you can use decimal.ToDouble().

Here are some examples:

// Example 1
double transperancy = trackBar1.Value/5000;
this.Opacity = decimal.ToDouble(transperancy);

// Example 2 - with inline temp
this.Opacity = decimal.ToDouble(trackBar1.Value/5000);
Answered on July 15, 2016.
Add Comment

You have two problems. First, Opacity requires a double, not a decimal value. The compiler is telling you that while there is a conversion between decimal and double, it is an explicit conversion that you need to specify in order for it to work. The second is that TrackBar.Value is an integer value and dividing an int by an int results in an int no matter what type of variable you assign it to. In this case there is an implicit cast from int to decimal or double – because there is no loss of precision when you do the cast – so the compiler doesn’t complain, but the value you get is always 0, presumably, since trackBar.Value is always less than 5000. The solution is to change your code to use double (the native type for Opacity) and do floating point arithmetic by explicitly making the constant a double – which will have the effect of promoting the arithmetic – or casting trackBar.Value to double, which will do the same thing – or both. Oh, and you don’t need the intermediate variable unless it used elsewhere. My guess is the compiler would optimize it away, anyway.

 trackBar.Opacity = (double)trackBar.Value / 5000.0;
Answered on July 15, 2016.
Add Comment

You should use 5000.0 instead of 5000.

Answered on July 15, 2016.
Add Comment

It sounds like this.Opacity is a double value, and the compiler doesn’t like you trying to cram a decimal value into it.

Answered on July 15, 2016.
Add Comment

Since Opacity is a double value, I would just use a double from the outset and not cast at all, but be sure to use a double when dividing so you don’t loose any precision

Opacity = trackBar1.Value / 5000.0;
Answered on July 15, 2016.
Add Comment

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

Answered on July 15, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.