如何在 C# 中将 Decimal 转换为 Double?

How to convert a Decimal to a Double in C#?(如何在 C# 中将 Decimal 转换为 Double?)
本文介绍了如何在 C# 中将 Decimal 转换为 Double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Track-Bar 来更改 Form 的不透明度.

这是我的代码:

十进制 trans = trackBar1.Value/5000;this.Opacity = 反式;

当我构建应用程序时,它给出了以下错误:

<块引用>

不能将十进制类型隐式转换为双精度

我曾尝试使用 transdouble,但随后 Control 不起作用.此代码在过去的 VB.NET 项目中运行良好.

解决方案

不需要像这样显式转换为 double:

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

将常量标识为5000.0(或5000d)就足够了:

double trans = trackBar1.Value/5000.0;双反 = trackBar1.Value/5000d;

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

这篇关于如何在 C# 中将 Decimal 转换为 Double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)