share
Stack OverflowHow to convert Decimal to Double in C#?
[+813] [13] Eggs McLaren
[2008-07-31 21:42:52]
[ c# floating-point type-conversion double decimal ]
[ https://stackoverflow.com/questions/4/how-to-convert-decimal-to-double-in-c ]

I want to assign the decimal variable trans to the double variable this.Opacity.

decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;

When I build the app it gives the following error:

Cannot implicitly convert type decimal to double

(21) Also, Decimal can't represent as wide a value as a Double. Decimal can only go up to +/-7.9228162514264337593543950335E+28; whereas a Double can go up to +/-1.79769313486232E+308 - TraumaPony
(4) @TraumaPony it's a trackbar. It is unlikely that it has ever been done to use such a high value on trackbar - Franck
(8) I was always told it is better to multiply by 0.0002 than divide by 5000. - JosephDoggie
(1) "trans" is probably for "transparency" (from an answer?), the opposite of opacity, indicated by the variable name "Opacity". - Peter Mortensen
[+533] [2008-07-31 22:17:57] Kevin Dente [ACCEPTED]

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;

1
[+154] [2008-08-01 14:23:28] huseyint

A more generic answer for the generic question "Decimal vs Double?":

Decimal is for monetary calculations to preserve precision. Double is for scientific calculations that do not get affected by small differences. Since Double is a type that is native to the CPU (internal representation is stored in base 2), calculations made with Double perform better than Decimal (which is represented in base 10 internally).


2
[+109] [2008-08-10 17:54:27] Keith

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 [1]" 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.

[1] http://bizvprog.blogspot.com/2008/05/floating-point-numbers-more-inaccurate.html

3
[+98] [2008-09-21 03:51:01] Gordon Bell

Why are you dividing by 5000? Just set the TrackBar's Minimum and Maximum values between 0 and 100 and then divide the Value by 100 for the Opacity percentage. The minimum 20 example below prevents the form from becoming completely invisible:

private void Form1_Load(object sender, System.EventArgs e)
{
    TrackBar1.Minimum = 20;
    TrackBar1.Maximum = 100;

    TrackBar1.LargeChange = 10;
    TrackBar1.SmallChange = 1;
    TrackBar1.TickFrequency = 5;
}

private void TrackBar1_Scroll(object sender, System.EventArgs e)
{
    this.Opacity = TrackBar1.Value / 100;
}

(9) Wouldn't this just move the problem around? Rather than a problem with 5000, OP would have a problem with 100? - jww
4
[+78] [2009-02-27 11:45:40] tvanfosson

You have two problems.

Firstly, 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.

Secondly, 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. You don't need the intermediate variable unless it is used elsewhere. My guess is the compiler would optimize it away anyway.

trackBar.Opacity = (double)trackBar.Value / 5000.0;

5
[+72] [2008-08-05 20:18:30] andynil

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 transparency = trackBar1.Value/5000;
this.Opacity = decimal.ToDouble(transparency);

// Example 2 - with inline temp
this.Opacity = decimal.ToDouble(trackBar1.Value/5000);

6
[+68] [2008-08-01 13:53:06] Ryan Fox

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


7
[+63] [2008-11-20 14:36:42] Dinah

You should use 5000.0 instead of 5000.


8
[+63] [2011-08-31 19:08:26] Darin Dimitrov

The Opacity [1] property is of double type:

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

or simply:

this.Opacity = trackBar1.Value / 5000.0;

or:

this.Opacity = trackBar1.Value / 5000d;

Notice that I am using 5000.0 (or 5000d) to force a double division because trackBar1.Value [2] is an integer and it would perform an integer division and the result would be an integer.

[1] https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.opacity
[2] https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.trackbar.value

9
[+58] [2011-08-31 19:09:50] ChrisF

Assuming you are using WinForms, Form.Opacity [1] 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, and because trackbar1.Value is also an integer 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.

[1] https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.opacity

10
[+52] [2012-05-13 02:10:25] Darryl

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 lose any precision:

Opacity = trackBar1.Value / 5000.0;

11
[+51] [2012-03-06 08:07:53] Danny Fox

The best solution is:

this.Opacity = decimal.ToDouble(trackBar1.Value/5000);

12
[+3] [2022-07-15 16:00:08] David Rodrigues

OG Fact: Double type represents a wider range of possible values than Decimal.

Casting as Double

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

Type Conversion

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

No explicit Cast/Conversion

In this case, adding 'd' in the end of the constant 5000d or ".0" 5000.0 will identify the required Type. When there is no constant in the operation, just multiply your decimal variable by 1.0 or 1d.


(1) What is "OG"? Is it "Initialism of Original Gangsta; a person associated with the earliest era of gangsta rap. ... by extension, US, slang, anything associated with an earlier, more traditional, and perceivedly more authentic era."? Or something else? - Peter Mortensen
(1) @PeterMortensen Old & Gold - David Rodrigues
13