-5
// here I set the discount which after the subtraction should be 150 euro         
public int SetDiscountPrice()
{
   return price = price -  (10/100)*price;
}

and here I call the method SetDicountPrice but it does not work

Console.WriteLine("\nAfter selling 1 item for {0} euro, number of sofas left is {1}",sofa.SetDiscountPrice(), sofa.SellOneProduct());
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131

3 Answers3

2

int / int = int so your calculation would be x - 0 = x because 10/100 equals 0

If you want to calcualte with floating point values use

return price = (int)(price - (10m / 100m) * price); // m for decimal type

or if you just want to get the value without changing the initial price

return (int)(price - (10m / 100m) * price); 

and think about using type decimal for money values

fubo
  • 44,811
  • 17
  • 103
  • 137
  • 1
    In case anyone is wondering why `m` stands for `decimal` [have a look here](https://stackoverflow.com/a/977562/2099119). – waka Sep 19 '17 at 12:32
  • I think you may need to cast the calculation to int if that is the return type (clue is in the error message). – PaulF Sep 19 '17 at 12:35
  • this return statement will not compile. C# compiler will force you to apply an explicit cast... – Mong Zhu Sep 19 '17 at 12:37
  • @MongZhu: This is why I will finish my career as a C and C++ programmer. I hate all these compulsory explicit conversions in C# and Java. – Bathsheba Sep 19 '17 at 12:39
  • @Bathsheba - stick with C/C++ they do more implicit casts, it's C# that requires the explicit casts – PaulF Sep 19 '17 at 12:40
  • @PaulF: I will! – Bathsheba Sep 19 '17 at 12:41
  • 1
    @MongZhu I've mentioned both ways - just seems to be bad practice to me – fubo Sep 19 '17 at 12:43
1

(10/100) is an expression equal to 0 due to integer division.

Given that you've told us that price is an integral type, you ought to use

return price = price - price / 10;

and be aware that this will always round up the result. Alternatively, use

return price = price * 9 / 10;

which will round downwards, but be mindful that this is vulnerable to integer overflow due to an order of magnitude increase in the int during the calculation.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Solution:

An easy fix would either be casting it to a decimal (Note that price has to be a decimal in that case):

return price = price - (decimal)10/100*price;

or use a decimal as the divisor:

return price = price - 10M/100M*price;

you obviously have to adjust your return type as well:

public decimal SetDiscountPrice(){
    return price = price - (decimal)10/100*price;
}

Important Information:

Maybe you should actually overthink your attempt a little bit. A method starting with "Set" sounds like it should be a property and I think it's definitely possible in your case. And are you sure you want your setter-method to change your value and return it at the same time? Maybe following could help you:

class YourClass{
    public decimal Price {get; set;}
    public decimal DiscountPrice => (decimal)10/100*Price;
}
Damien Flury
  • 769
  • 10
  • 23
  • same here: `return price = price - (double)10/100*price;` does not compile and neither does `return price = price - 10d/100d*price;`. No implicit converstion! – Mong Zhu Sep 19 '17 at 12:39
  • 1
    Using `double` for money types = very, very naughty especially as C# has a nice shiny built-in decimal type. – Bathsheba Sep 19 '17 at 12:40
  • @Bathsheba yeah you're right. I will change it to decimal – Damien Flury Sep 19 '17 at 12:43
  • @MongZhu Yep, if price isn't the correct type it obviously won't work. He should use a decimal in this case. If he doesn't care about floating points he should round it. – Damien Flury Sep 19 '17 at 12:49
  • in a comment OP states "in the description, it is given as an integer". So I guess it is part of the excersize. # – Mong Zhu Sep 19 '17 at 12:54