开发者

How to literally define an array of decimals without multiple casting?

开发者 https://www.devze.com 2023-01-05 17:30 出处:网络
How can I define an array of decimals without explicitly casting each one? //decimal[] prices = { 39.99, 29.99, 29.99, 19.99, 49.99 }; //can\'t convert double to decimal

How can I define an array of decimals without explicitly casting each one?

//decimal[] prices = { 39.99, 29.99, 29.99, 19.99, 49.99 }; //can't convert double to decimal
//var prices = { 39.99, 29.99, 29.9开发者_JAVA百科9, 19.99, 49.99 }; //can't initialize... 
decimal[] prices = { (decimal)39.99, (decimal)29.99, (decimal)29.99, (decimal)19.99, (decimal)49.99 };


Use the m suffix.

decimal[] prices = { 39.99m, 29.99m, 19.99m, 49.99m };

Without the m (or M) suffix, the compiler treats it as a double.

-- http://msdn.microsoft.com/en-us/library/364x0z75(VS.71).aspx


You need to add an M to the end


A decimal literal is 39.99m

0

精彩评论

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