You’re making an unnecessary conversion and too late. When you make a division with integers, you will have the result as an integer, and the integer of the obtained result is 0. If you want the decimal part you need to use a given decimal, be it for a variable that is already decimal
or a literal of that number. The literal of the type decimal
always accompanied by the suffix M
of money, since this type is usually used for monetary value. Without the suffix is by default an integer, and there is problem. Thus should give the expected result:
using static System.Console;
public class Program {
public static void Main() {
decimal contagemSubida = 0 , contagemDescida = 0;
int cSubida = 6, cDescida = 4, range = 10;
contagemSubida += cSubida * range / 100M;
contagemDescida += cDescida * range / 100M;
contagemSubida += contagemSubida * (4 / 10M);
contagemDescida += contagemDescida * (4 / 10M);
WriteLine(contagemSubida);
WriteLine(contagemDescida);
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Note that if you change the type of the secondary variables to decimal
the result can be another without changing the divisor to the type decimal
and it doesn’t seem to be what you want, although I can’t say because the question doesn’t say what the outcome should be.
Perfect! I understood the line of reasoning, applied and gave right :D
– Thiago Oliveira