开发者

double? to decimal? conversion

开发者 https://www.devze.com 2023-01-26 16:48 出处:网络
Why after this code fragment: double? d = 123.4; decimal? dec = (decimal?)d; //dec is null is dec\'s v开发者_StackOverflow中文版alue null?I\'ve tested on .NET 2.0 and .NET 4.0, and I get dec as 1

Why after this code fragment:

 double? d = 123.4;
 decimal? dec = (decimal?)d; //dec is null

is dec's v开发者_StackOverflow中文版alue null?


I've tested on .NET 2.0 and .NET 4.0, and I get dec as 123.4, as expected. Are you sure you don't perhaps have the break-point too high?


dec isn't null, its 123.4


I know this will point same thing. Your code gives me 123,4 as expected. It works fine.

I can just say try this :) but the result is same : 123,4

double? d = 123.4;
 decimal? dec = (decimal?)d.Value;


My best guess is that the implicit conversion being called in the cast fails, leaving dec with a null value, rather than whatever truncated/rounded value you expect.


It's not null. You must have an error elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消