HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How to convert Decimal to Double in C#?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
doubleconvertdecimalhow

Problem

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

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;

Code Snippets

double trans = (double) trackBar1.Value / 5000.0;
double trans = trackBar1.Value / 5000.0;
double trans = trackBar1.Value / 5000d;

Context

Stack Overflow Q#4, score: 539

Revisions (0)

No revisions yet.