<p>I want to assign the decimal variable &quot;trans&quot; to the double variable &quot;this.Opacity&quot;.</p>
<pre class="lang-cs prettyprint-override"><code>decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
</code></pre>
<p>When I build the app it gives the following error:</p>
<blockquote>
<p>Cannot implicitly convert type decimal to double</p>
</blockquote>

[{'answer_id': 7, 'author': 'Kevin Dente', 'author_id': 9, 'author_profile': 'https://Stackoverflow.com/users/9', 'pm_score': 10, 'selected': True, 'text': "<p>An explicit cast to <code>double</code> like this isn't necessary:</p>\n\n<pre><code>double trans = (double) trackBar1.Value / 5000.0;\n</code></pre>\n\n<p>Identifying the constant as <code>5000.0</code> (or as <code>5000d</code>) is sufficient:</p>\n\n<pre><code>double trans = trackBar1.Value / 5000.0;\ndouble trans = trackBar1.Value / 5000d;\n</code></pre>\n"}, {'answer_id': 78, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 6, 'selected': False, 'text': "<p>It sounds like <code>this.Opacity</code> is a double value, and the compiler doesn't like you trying to cram a decimal value into it.</p>\n"}, {'answer_id': 86, 'author': 'huseyint', 'author_id': 39, 'author_profile': 'https://Stackoverflow.com/users/39', 'pm_score': 7, 'selected': False, 'text': '<p><strong>A more generic answer for the generic question &quot;Decimal vs Double?&quot;:</strong></p>\n<p><strong>Decimal</strong> is for monetary calculations to preserve precision. <strong>Double</strong> 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 <em>base 2</em>), calculations made with Double perform better than Decimal (which is represented in <em>base 10</em> internally).</p>\n'}, {'answer_id': 2791, 'author': 'andynil', 'author_id': 446, 'author_profile': 'https://Stackoverflow.com/users/446', 'pm_score': 6, 'selected': False, 'text': '<p>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.</p>\n<p>In addition to (or instead of) appending a <code>.0</code> to the number, you can use <code>decimal.ToDouble()</code>.</p>\n<p>Here are some examples:</p>\n<pre><code>// Example 1\ndouble transparency = trackBar1.Value/5000;\nthis.Opacity = decimal.ToDouble(transparency);\n\n// Example 2 - with inline temp\nthis.Opacity = decimal.ToDouble(trackBar1.Value/5000);\n</code></pre>\n'}, {'answer_id': 7263, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 7, 'selected': False, 'text': '<p>Your code worked fine in VB.NET because it implicitly does any casts, while C# has both implicit and explicit ones.</p>\n\n<p>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 "<a href="http://bizvprog.blogspot.com/2008/05/floating-point-numbers-more-inaccurate.html" rel="noreferrer">Floating point numbers - more inaccurate than you think</a>" for the reason why).</p>\n\n<p>In VB the conversion was added for you by the compiler:</p>\n\n<pre><code>decimal trans = trackBar1.Value / 5000m;\nthis.Opacity = (double) trans;\n</code></pre>\n\n<p>That <code>(double)</code> has to be explicitly stated in C#, but can be <em>implied</em> by VB\'s more \'forgiving\' compiler.</p>\n'}, {'answer_id': 110198, 'author': 'Gordon Bell', 'author_id': 16473, 'author_profile': 'https://Stackoverflow.com/users/16473', 'pm_score': 7, 'selected': False, 'text': "<p>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:</p>\n\n<pre><code>private void Form1_Load(object sender, System.EventArgs e)\n{\n    TrackBar1.Minimum = 20;\n    TrackBar1.Maximum = 100;\n\n    TrackBar1.LargeChange = 10;\n    TrackBar1.SmallChange = 1;\n    TrackBar1.TickFrequency = 5;\n}\n\nprivate void TrackBar1_Scroll(object sender, System.EventArgs e)\n{\n    this.Opacity = TrackBar1.Value / 100;\n}\n</code></pre>\n"}, {'answer_id': 305467, 'author': 'Dinah', 'author_id': 356, 'author_profile': 'https://Stackoverflow.com/users/356', 'pm_score': 6, 'selected': False, 'text': '<p>You should use <code>5000.0</code> instead of <code>5000</code>.</p>\n'}, {'answer_id': 594436, 'author': 'tvanfosson', 'author_id': 12950, 'author_profile': 'https://Stackoverflow.com/users/12950', 'pm_score': 6, 'selected': False, 'text': "<p>You have two problems.</p>\n<p>Firstly, <code>Opacity</code> 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.</p>\n<p>Secondly, <code>TrackBar.Value</code> 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 <code>trackBar.Value</code> is always less than 5000.</p>\n<p>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 <code>trackBar.Value</code> 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.</p>\n<pre><code>trackBar.Opacity = (double)trackBar.Value / 5000.0;\n</code></pre>\n"}, {'answer_id': 7262112, 'author': 'Darin Dimitrov', 'author_id': 29407, 'author_profile': 'https://Stackoverflow.com/users/29407', 'pm_score': 6, 'selected': False, 'text': '<p>The <a href="https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.opacity" rel="noreferrer">Opacity</a> property is of double type:</p>\n\n<pre><code>double trans = trackBar1.Value / 5000.0;\nthis.Opacity = trans;\n</code></pre>\n\n<p>or simply:</p>\n\n<pre><code>this.Opacity = trackBar1.Value / 5000.0;\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>this.Opacity = trackBar1.Value / 5000d;\n</code></pre>\n\n<p>Notice that I am using <code>5000.0</code> (or <code>5000d</code>) to force a double division because <a href="https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.trackbar.value" rel="noreferrer"><code>trackBar1.Value</code></a> is an integer and it would perform an integer division and the result would be an integer.</p>\n'}, {'answer_id': 7262131, 'author': 'ChrisF', 'author_id': 59303, 'author_profile': 'https://Stackoverflow.com/users/59303', 'pm_score': 6, 'selected': False, 'text': '<p>Assuming you are using WinForms, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.opacity" rel="noreferrer"><code>Form.Opacity</code></a> is of type <code>double</code>, so you should use:</p>\n\n<pre><code>double trans = trackBar1.Value / 5000.0;\nthis.Opacity = trans;\n</code></pre>\n\n<p>Unless you need the value elsewhere, it\'s simpler to write:</p>\n\n<pre><code>this.Opacity = trackBar1.Value / 5000.0;\n</code></pre>\n\n<p>The reason the control doesn\'t work when you changed your code to simply be a double was because you had:</p>\n\n<pre><code>double trans = trackbar1.Value / 5000;\n</code></pre>\n\n<p>which interpreted the <code>5000</code> as an integer, and because <code>trackbar1.Value</code> is also an integer your <code>trans</code> value was always zero. By explicitly making the numeric a floating point value by adding the <code>.0</code> the compiler can now interpret it as a double and perform the proper calculation.</p>\n'}, {'answer_id': 9579950, 'author': 'Danny Fox', 'author_id': 1091828, 'author_profile': 'https://Stackoverflow.com/users/1091828', 'pm_score': 6, 'selected': False, 'text': '<p>The best solution is:</p>\n\n<pre><code>this.Opacity = decimal.ToDouble(trackBar1.Value/5000);\n</code></pre>\n'}, {'answer_id': 10568821, 'author': 'Darryl', 'author_id': 1391700, 'author_profile': 'https://Stackoverflow.com/users/1391700', 'pm_score': 6, 'selected': False, 'text': "<p>Since <code>Opacity</code> 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:</p>\n<pre><code>Opacity = trackBar1.Value / 5000.0;\n</code></pre>\n"}, {'answer_id': 71840515, 'author': 'Arnold Brown', 'author_id': 5049244, 'author_profile': 'https://Stackoverflow.com/users/5049244', 'pm_score': 1, 'selected': False, 'text': '<p><strong>Try the following code:</strong></p>\n<h3>Decimal values</h3>\n<pre><code>decimal d1 = 3234.3434m;\n</code></pre>\n<h3>Convert to double</h3>\n<pre><code>double r1 = Decimal.ToDouble(d1);\n</code></pre>\n'}, {'answer_id': 72996737, 'author': 'David Rodrigues', 'author_id': 5768360, 'author_profile': 'https://Stackoverflow.com/users/5768360', 'pm_score': 1, 'selected': False, 'text': "<p>OG Fact: <code>Double</code> type represents a wider range of possible values than <code>Decimal</code>.</p>\n<p><strong>Casting as Double</strong></p>\n<pre><code>decimal trans = trackBar1.Value / 5000m;\nthis.Opacity = (double) trans;\n</code></pre>\n<p><strong>Type Conversion</strong></p>\n<pre><code>decimal trans = trackBar1.Value / 5000m;\nthis.Opacity = decimal.ToDouble(trans);\n</code></pre>\n<p><strong>No explicit Cast/Conversion</strong></p>\n<p>In this case, adding 'd' in the end of the constant <code>5000d</code> or &quot;.0&quot; <code>5000.0</code> will identify the required Type. When there is no constant in the operation, just multiply your <code>decimal</code> variable by <code>1.0</code> or <code>1d</code>.</p>\n"}]
['https://Stackoverflow.com/questions/4', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8/']