Variable remains zero in a mathematical expression

1

My program looks like this:

        public void CalculaProporção()
    {
        decimal contagemSubida = 0 , contagemDescida = 0;
        int cSubida = 6, cDescida = 4, range = 10;
        contagemSubida += Convert.ToDecimal(cSubida*range/100);
        contagemDescida += Convert.ToDecimal(cDescida*range/100);

        contagemSubida = contagemSubida + Convert.ToDecimal(contagemSubida * (4/10));
        contagemDescida = contagemDescida + Convert.ToDecimal(contagemDescida * (4/10));
    }

My sub-count and count-down variables are set to 0 throughout the procedure, what should I do to make them calculate correctly?

    
asked by anonymous 01.11.2018 / 15:54

2 answers

2

You are doing an unnecessary conversion and too late. When you divide with integers, you will have resulted as an integer, and the integer of the result obtained is 0. If either the decimal part needs to use a given decimal, either by a variable that is already decimal or a literal of that number. The literal of type decimal always comes with the suffix M of money , since this type is usually used for monetary value. Without the suffix it is by default an integer, and then there is trouble. So you should give the result you expect:

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);
    }
}

See running on .NET Fiddle . And at Coding Ground . Also I placed GitHub for future reference .

Note that if you change the type of the secondary variables to decimal the result may be another without changing the divisor to type decimal and it does not seem to be what you want, although I can not state why the question does not say which should be the result.

    
01.11.2018 / 16:07
1

I do not know if it was your broker but you do not usually use accentuation in programming.

Change the data type to decimal:

decimal cSubida = 6, cDescida = 4, range = 10;

Example: link

    
01.11.2018 / 16:07