## Convert Decimal to 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 build the application, it gives the following error:

Cannot implicitly convert type

`decimal`

to`double`

I tried using `trans`

and `double`

but then the control doesn't work. This code worked fine in a past VB.NET project.

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;

**Decimal.ToDouble(Decimal) Method (System),** You answered your own question—Just cast it to a double: decimal x = 3.141592654M ; double pi = (double) x ;. This method is used to convert the value of the specified Decimal to the equivalent double-precision floating-point number. This method can produce round-off errors as a double-precision floating-point number has few significant digits than a Decimal. Here, d is the decimal value which is to be converted. Return value: It returns a double

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

**Converting Decimal to Double in C#?,** Built in casts for the win! Just tested this in VS2012 and VS2010: decimal? numberDecimal = new Decimal(5); decimal? nullDecimal = null public void ConvertDoubleDecimal(decimal decimalVal){ double doubleVal; // Decimal to double conversion cannot overflow. doubleVal = System.Convert.ToDouble(decimalVal); System.Console.WriteLine("{0} as a double is: {1}", decimalVal, doubleVal); // Conversion from double to decimal can overflow.

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.

**Convert decimal? to double?,** Convert decimal to double in C#. ConvertDataTypes is the helpfull website for converting your data types in several programming languages. About the Decimal to Floating-Point Converter. This is a decimal to binary floating-point converter. It will convert a decimal number to its nearest single-precision and double-precision IEEE 754 binary floating-point number, using round-half-to-even rounding (the default IEEE rounding mode).

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

**Convert decimal to double in C#,** Convert Decimal to Double in VB net. ConvertDataTypes is the helpfull website for converting your data types in several programming languages. Converts a specified value to a double-precision floating-point number. Converts the value of the specified 8-bit signed integer to the equivalent double-precision floating-point number. Converts the value of the specified object to an double-precision floating-point number, using the specified culture-specific formatting information.

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;

**Convert Decimal to Double in VB.net,** If you disable high precision, Data Integration converts decimal values to double. Precision loss occurs if the decimal value has a precision greater than 15 digits Binary Decimal Hexadecimal. Binary to Decimal conversion How to convert decimal to binary. Conversion steps: Divide the number by 2. Get the integer quotient for the next iteration. Get the remainder for the binary digit. Repeat the steps until the quotient is equal to 0. Convert 13 10 to binary: So 13 10 = 1101 2. Convert 174 10 to binary:

**Decimal and double values in calculations,** Supports all types of variables, including single and double precision This converter allows you to convert numbers from decimal format to binary format and To use this decimal to hex converter tool, you have to type a decimal value like 79 into the left field below, and then hit the Convert button. Therefore, you can convert up to 19 decimal characters (max. value of 9223372036854775807) to hex. The decimal numeral system is the most commonly used and the standard system in daily life.

**Online Binary-Decimal Converter,** Learn how to convert a Double to a String while removing the fractional digits using a number of different approaches. Input a decimal number to convert both the whole number and decimal part of the number to a percent value. How to Convert a Decimal to Percent: Multiply by 100 to convert a number from decimal to percent then add a percent sign %. Converting from a decimal to a percentage is done by multiplying the decimal value by 100 and adding %.

**Convert Double to String, Removing Decimal Places,** It will convert a decimal number to its nearest single-precision and double-precision IEEE 754 binary floating-point number, using round-half-to-even rounding (the Floating Point to Hex Converter. Check out the new Windows and Windows Phone apps! Here are the Python files that are needed to make your own: