C# 将十进制转换为双精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Decimal to Double
提问by Eggs McLaren
I want to use a Track-Bar
to change a Form
's opacity.
我想用 aTrack-Bar
来改变 aForm
的不透明度。
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
todouble
不能将类型隐式转换
decimal
为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.
我试过使用trans
,double
但后来Control
不起作用。此代码在过去的 VB.NET 项目中运行良好。
采纳答案by Kevin Dente
An explicit cast to double
like this isn't necessary:
double
不需要像这样的显式转换:
double trans = (double) trackBar1.Value / 5000.0;
Identifying the constant as 5000.0
(or as 5000d
) is sufficient:
将常量标识为5000.0
(或 as 5000d
) 就足够了:
double trans = trackBar1.Value / 5000.0;
double trans = trackBar1.Value / 5000d;
回答by Ryan Fox
It sounds like this.Opacity
is a double value, and the compiler doesn't like you trying to cram a decimal value into it.
这听起来像是this.Opacity
一个双精度值,编译器不喜欢您试图将十进制值塞入其中。
回答by huseyint
A more generic answer for the generic question "Decimal vs Double?": Decimalfor monetary calculations to preserve the precision, Doublefor 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 10internally).
通用问题“Decimal vs Double?”的更通用答案:用于货币计算以保持精度的Decimal,Double用于不受微小差异影响的科学计算。由于 Double 是 CPU 固有的类型(内部表示存储在基数 2 中),因此使用 Double 进行的计算比 Decimal 执行得更好(内部表示在基数 10 中)。
回答by andnil
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()
.
除了(或代替)将 a 添加.0
到数字之外,您还可以使用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);
回答by Keith
Your code worked fine in VB.NET because it implicitly does any casts, while C# has both implicit and explicit ones.
您的代码在 VB.NET 中运行良好,因为它隐式执行任何类型转换,而 C# 具有隐式和显式转换。
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).
在 C# 中,当您失去准确性时,从十进制到双精度的转换是明确的。例如,1.1 不能准确地表示为双精度数,但可以表示为小数(请参阅“浮点数 - 比您想象的更不准确”原因)。
In VB the conversion was added for you by the compiler:
在 VB 中,编译器为您添加了转换:
decimal trans = trackBar1.Value / 5000m;
this.Opacity = (double) trans;
That (double)
has to be explicitly stated in C#, but can be impliedby VB's more 'forgiving' compiler.
这(double)
必须在 C# 中明确说明,但可以由 VB 更“宽容”的编译器暗示。
回答by Gordon Bell
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:
为什么要除以 5000?只需将 TrackBar 的最小值和最大值设置在 0 到 100 之间,然后将值除以 100 作为不透明度百分比。下面的最少 20 个示例可防止表单完全不可见:
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;
}
回答by Dinah
You should use 5000.0
instead of 5000
.
你应该使用5000.0
而不是5000
.
回答by tvanfosson
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.
你有两个问题。首先,Opacity
需要一个双精度值,而不是十进制值。编译器告诉您,虽然十进制和双精度之间存在转换,但您需要指定显式转换才能使其正常工作。第二个是它TrackBar.Value
是一个整数值,无论你将它分配给什么类型的变量,用一个 int 除以一个 int 都会得到一个 int。在这种情况下,有一个从 int 到 decimal 或 double 的隐式转换——因为在你进行转换时没有精度损失——所以编译器不会抱怨,但你得到的值总是 0,大概是因为trackBar.Value
始终小于 5000。解决方案是更改您的代码以使用 double(Opacity 的本机类型)并通过显式将常量设置为 double 来执行浮点运算 - 这将具有促进算术的效果 - 或强制转换trackBar.Value
为 double ,这将做同样的事情 - 或两者兼而有之。哦,您不需要中间变量,除非它在其他地方使用。我的猜测是编译器会优化它,无论如何。
trackBar.Opacity = (double)trackBar.Value / 5000.0;
回答by Darin Dimitrov
The Opacityproperty 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.
请注意,我使用5000.0
(or 5000d
) 强制进行双除,因为trackBar1.Value
是整数,它将执行整数除法,结果将是整数。
回答by ChrisF
Assuming you are using WinForms, Form.Opacity
is of type double
, so you should use:
假设您使用的是 WinForms,Form.Opacity
类型为double
,因此您应该使用:
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 时控件不起作用的原因是因为您有:
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.
它将 the 解释5000
为一个整数,并且因为trackbar1.Value
它也是一个整数,所以您的trans
值始终为零。通过添加显式使数字成为浮点值,.0
编译器现在可以将其解释为双精度值并执行正确的计算。