Answer the question
In order to leave comments, you need to log in
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
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).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question