Cannot implicitly convert "decimal" type to "int" in C#function code

Asked

Viewed 253 times

5

This code averages the arithmetic or weighted average of the 3 student grades based on your choice of average according to the use of the Equals, code error is exactly this conversion of decimal for int, how to correctly perform this conversion?

using static System.Console;

namespace Função1
{
    class Class4
    {
        static int Main(string[] args)
        {
            decimal nota1 = 0, nota2 = 0, nota3 = 0;
            string letra;
            Write("Digite a 1ª nota: ");
            nota1 = decimal.Parse(ReadLine());
            Write("Digite a 2ª nota: ");
            nota2 = decimal.Parse(ReadLine());
            Write("Digite a 3ª nota: ");
            nota3 = decimal.Parse(ReadLine());
            Write("Digite ou A para calcular a média aritmética ou P para calcular a média ponderada: ");
            letra = ReadLine();
            WriteLine($"{Calcula(nota1, nota2, nota3, letra)}");
            ReadKey();
            return 0;
        }

        static int Calcula(decimal nota1, decimal nota2, decimal nota3, string letra)
        {
            decimal media = 0;
            if (letra.Equals("A"))
            {
                media = (nota1 + nota2 + nota3) / 3;
                Write("A opção digitada foi um média aritmética");
            }
            else if (letra.Equals("P"))
            {
                media = ((nota1 * 5) + (nota2 * 3) + (nota3 * 2)) / (5 + 3 + 2);
                Write("A opção digitada foi um média pnderada");
            }
            return media;
        }
    }
}
  • Carlos, you want the average to be an integer or decimal?

  • The average in these cases has to be decimal

3 answers

7


It’s the same problem as previous question but now the solution is different. The return type of the method has to match what is being returning, so if the method really has to return int return int and not decimal, if on the other hand it must return decimal then change the type of return of the method, shrinks which is the best solution to the problem, it seems to me that is change the type of return of the method to decimal, saddlebag like this:

static decimal Calcula(decimal nota1, decimal nota2, decimal nota3, string letra)

I put in the Github for future reference.

Do any of these two things but it makes no sense to convert the number decimal for int because you’re throwing away a part of the calculated amount. I think you want to keep Alor calculated, but if you didn’t want to, then everything should be whole and not waste resources to throw away what you got (I don’t say this because of the question but because of the other answer).

I will not try to solve the other problems of the code, for example the use of Equals() which is not enough to be a mistake, but is not idiomatic. For example this should be two methods and not a parameterized what to do.

  • I’m solving these other problems

4

I revisited the issue improving some values and was like this:

using static System.Console;

namespace Função1
{
    class Class4
    {
        static int Main(string[] args)
        {
            // Utilize vírgula nos valores decimais(Ex: 7, 5 ao invés de 7.5)
            string letra;
            Write("Digite a 1ª nota: ");
            if (!decimal.TryParse(ReadLine(), out decimal nota1)) return 1;
            Write("Digite a 2ª nota: ");
            if (!decimal.TryParse(ReadLine(), out decimal nota2)) return 1;
            Write("Digite a 3ª nota: ");
            if (!decimal.TryParse(ReadLine(), out decimal nota3)) return 1;
            Write("Digite ou A para calcular a média aritmética ou P para calcular a média ponderada: ");
            letra = ReadLine();
            WriteLine($"Média: {Calcula(nota1, nota2, nota3, letra)}");
            ReadKey();
            return 0;
        }

        static decimal Calcula(decimal nota1, decimal nota2, decimal nota3, string letra)
        {
            decimal media = 0;
            if (letra.Equals("A"))
            {
                media = (nota1 + nota2 + nota3) / 3;
                WriteLine("A opção digitada foi um média aritmética");
            }
            else if (letra.Equals("P"))
            {
                media = ((nota1 * 5) + (nota2 * 3) + (nota3 * 2)) / (5 + 3 + 2);
                WriteLine("A opção digitada foi um média ponderada");
            }
            return System.Math.Round(media, 2);
        }
    }
}

3

How to properly perform this conversion?

Simply convert from decimal for int:

return Decimal.ToInt32(media);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.