C#中的十进制数据类型 [英] decimal data type in c#

查看:191
本文介绍了C#中的十进制数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double vdouble;
十进制vdecimal;
vdouble = vdecimal;//不能从十进制隐式转换为double.

我的问题是,

double vdouble;
decimal vdecimal;
vdouble=vdecimal;//cannot implicitaly convert from decimal to double.

my question is that,
if double has larger range than decimal then why implicit conversion is occurred..

推荐答案

C#不允许您在浮点数字类型&内进行隐式转换.整数数据类型,您需要明确地告诉它进行转换.这主要是为了使您知道要在不一定容易转换的类型之间进行转换(十进制和双精度具有不同的范围和位深度).

隐式转换[ 显式转换 [
C# does not allow you to implicitly convert between floating point numeric types & integer data types, you need to explicitly tell it to convert. This is mostly to make you aware that you''re converting between types that don''t necessarily convert easily (decimal and double have different ranges and bit depths).

Implicit Conversion[^]
Explicit Conversion[^]

This will work because they are from Integer family & long is larger than int. implicit conversion give you guarantee that you are not going to lose your data.
int i = 50;
long lng = 100;
lng = i;



在这里,您需要显式转换数据类型,因为int小于long.
因此编译器知道您已准备好释放一些数据.



Here you need to explicitly convert the data type because the int is smaller than long.
so compiler knows that you are ready to loose some data.

int i = 50;
long lng = 3147483647;
i = (int) lng;



在您的情况下,无论十进制范围是多少,双重,编译器始终会向您显示错误消息,因为它们属于不同的数据类型家族.
http://stackoverflow.com/a/7817957 [



In your scenario whatever there ranges of decimal & double, compiler always gives you error message because they belongs to different data type family.
http://stackoverflow.com/a/7817957[^]


检查以下内容:
Check this: http://stackoverflow.com/questions/1584314/conversion-of-a-decimal-to-double-number-in-c-sharp-results-in-a-difference[^]


double vdouble;
decimal vdecimal=10;
vdouble = Convert.ToDouble(vdecimal);



双精度和十进制是不同的数据类型,因此不能



double and decimal are different datatypes so it can''t


这篇关于C#中的十进制数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆