Convert Decimal to Double?

2019-01-04 05:38发布

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.

13条回答
放我归山
2楼-- · 2019-01-04 06:00

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;
查看更多
孤傲高冷的网名
3楼-- · 2019-01-04 06:02

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;
查看更多
姐就是有狂的资本
4楼-- · 2019-01-04 06:03

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-04 06:04

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;
}
查看更多
老娘就宠你
6楼-- · 2019-01-04 06:06

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

查看更多
Fickle 薄情
7楼-- · 2019-01-04 06:07

The best solution is:

this.Opacity = decimal.ToDouble(trackBar1.Value/5000);
查看更多
登录 后发表回答