R
R
rad952018-11-01 18:21:01
Mathematics
rad95, 2018-11-01 18:21:01

How to make it so that division would be a decimal number in C#?

C#. It is necessary for me that at function call Div(6, 4) (for example, ie not necessarily 6 and 4) value 1.5 is returned. But something else is returned; \ If anything, other arithmetic operations have not been completed

static decimal Div(int a, int b)
        {
            decimal sum = a / b;
            return sum;


        }
        static void Main(string[] args)
        {
            Console.WriteLine("Введите действие. / * + -");
            string Znak = Console.ReadLine();
            if (Znak == "/")
            {
                Console.WriteLine("Введите делимое");
                int a = int.Parse(Console.ReadLine());
                Console.WriteLine("Введите делитель");
                int b = int.Parse(Console.ReadLine());
                while (b == 0)
                {
                    Console.WriteLine("Делитель должен быть больше 0. Введите число больше 0");
                    b = int.Parse(Console.ReadLine());

                }
                Console.WriteLine("{0} / {1} = {2}", a, b, Div(a, b));
            
            }
            Console.ReadKey();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2018-11-01
@rad95

and what's the problem?
you just need to divide not two ints, but convert it to decimal .. you can do one or both .. type casting and all that) in your case, when dividing an int by an int, you get an int (already converted to an integer) and only then cast it to sum(decimal).

K
Karpion, 2018-12-08
@Karpion

There is no number system here. You need to switch to floating arithmetic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question