c# – 不能将operator *应用于decimal和double类型的 *** 作数

c# – 不能将operator *应用于decimal和double类型的 *** 作数,第1张

概述嗨,我需要在产品上申请5.2%的折扣.我尝试过这样的事情: decimal BasePrice {get;set;}decimal Discount = (BasePrice * 5.2) / 100; 但Visual Studio告诉我它: can not apply operator ‘*’ to operand of type decimal and double 如果是这样我怎么能代表这 嗨,我需要在产品上申请5.2%的折扣.我尝试过这样的事情:
decimal BasePrice {get;set;}decimal discount = (BasePrice * 5.2) / 100;

但Visual Studio告诉我它:

can not apply operator ‘*’ to operand of type decimal and double

如果是这样我怎么能代表这个折扣?

解决方法 使用
decimal discount = (BasePrice * 5.2m) / 100;

否则,5.2将被视为双精度.

从MSDN开始:

If you want a numeric real literal to be treated as decimal,use the suffix m or M

总结

以上是内存溢出为你收集整理的c# – 不能将operator *应用于decimal和double类型 *** 作数全部内容,希望文章能够帮你解决c# – 不能将operator *应用于decimal和double类型的 *** 作数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存