开发者

More on Implied Types

开发者 https://www.devze.com 2023-01-18 16:57 出处:网络
As a follow up to the question what is the purpose of double implying... Also, I read in an article a while back (no I don\'t remember the link) that it is inappropriate to 开发者_如何学Godo decimal

As a follow up to the question what is the purpose of double implying...

Also, I read in an article a while back (no I don't remember the link) that it is inappropriate to 开发者_如何学Godo decimal dollars = 0.00M;. It stated that the appropriate way was decimal dollars = 0;. It also stated that this was appropriate for all numeric types. Is this incorrect, and why? If so, what is special about 0?


Well, for any integer it's okay to use an implicit conversion from int to decimal, so that's why it compiles. (Zero has some other funny properties... you can implicitly convert from any constant zero expression to any enum type. It shouldn't work with constant expressions of type double, float and decimal, but it happens to do so in the MS compiler.)

However, you may want to use 0.00m if that's the precision you want to specify. Unlike double and float, decimal isn't normalized - so 0m, 0.0m and 0.00m have different representations internally - you can see that when you call ToString on them. (The integer 0 will be implicitly converted to the same representation as 0m.)

So the real question is, what do you want to represent in your particular case?

0

精彩评论

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

关注公众号