Search code examples
c#doubledecimalpercentage

c# wrong number when calculating percentage


trying to calculate and subtract the price of something, but keeps coming up wrong, and adds an extra 500m to it and cannot understand why.

here is my code

        var discountPercentage = 50 / 100;
        discountedPrice= (decimal)(discountPercentage * 150000M + 10000M +  0);

the answer keeps giving me 85000.0M where it should be 80000, which is half of the combined amount (150000M + 10000M)


Solution

  • I think your parenthesis are placed wrong in discountPercentage. It has to do with priority of operations.

    discountPercentage * (150000M + 10000M +  0);