Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
It keeps telling me that i cant use these operators on the double,decimal,int data types. i know i can but how?


C#
public decimal Total { get; set; }
        public decimal Discount { get
            {
            double dis=0;
            dis=Total-(Total*0.05);
             }
            set; }
        public decimal Deposit { get; set; }




ive been told that there is a 'using'reference i need to use.

I am new to MVC5
Posted
Comments
Ajith K Gatty 28-May-14 6:25am    
You are new to MVC5. Ok. Are you even new too OOP Concepts??

1 solution

You are mixing double (dis) and decimal (Total) in your calculation so you are getting an error
Quote:
Operator '*' cannot be applied to operands of type 'decimal' and 'double'

This is because 0.05 is of type double. To convert it to decimal add an 'M' to the end of it like this
dis = Total - (Total * 0.05M);

However you are still mixing double and decimal values and will then get a different error
Quote:
Cannot implicitly convert type 'decimal' to 'double'. An explicit conversion exists (are you missing a cast?)
To get around that cast the result of the calculation to a double
dis = (double)(Total - (Total * 0.05M));
or better yet, be consistent with your variable types
 
Share this answer
 
v3
Comments
[no name] 28-May-14 11:20am    
Short and clear and didactic. My 5.
CHill60 28-May-14 12:51pm    
Thank you.

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