Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
Posted

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.
C#
int i = 50;
long lng = 100;
lng = i;


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.

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


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[^]
 
Share this answer
 
v2
C#
double vdouble;
decimal vdecimal=10;
vdouble = Convert.ToDouble(vdecimal);


double and decimal are different datatypes so it can't
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900