How to convert a Decimal to a Double in C#?

C#Floating PointType ConversionDoubleDecimal

C# Problem Overview


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:

> lang-none > Cannot implicitly convert type decimal to double >

I have tried using trans and double, but then the Control doesn't work. This code worked fine in a past VB.NET project.

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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;
}

Solution 5 - C#

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;

Solution 6 - C#

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

Solution 7 - C#

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

Solution 8 - C#

The Opacity 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 is an integer and it would perform an integer division and the result would be an integer.

Solution 9 - C#

You should use 5000.0 instead of 5000.

Solution 10 - C#

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

Solution 11 - C#

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;

Solution 12 - C#

The best solution is:

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

Solution 13 - C#

Try this -

// decimal values

decimal d1 = 3234.3434m;

// convert to double

double r1 = Decimal.ToDouble(d1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEggs McLarenView Question on Stackoverflow
Solution 1 - C#Kevin DenteView Answer on Stackoverflow
Solution 2 - C#huseyintView Answer on Stackoverflow
Solution 3 - C#KeithView Answer on Stackoverflow
Solution 4 - C#Gordon BellView Answer on Stackoverflow
Solution 5 - C#tvanfossonView Answer on Stackoverflow
Solution 6 - C#andynilView Answer on Stackoverflow
Solution 7 - C#Ryan FoxView Answer on Stackoverflow
Solution 8 - C#Darin DimitrovView Answer on Stackoverflow
Solution 9 - C#DinahView Answer on Stackoverflow
Solution 10 - C#ChrisFView Answer on Stackoverflow
Solution 11 - C#DarrylView Answer on Stackoverflow
Solution 12 - C#Danny FoxView Answer on Stackoverflow
Solution 13 - C#Arnold BrownView Answer on Stackoverflow