
You have two values in your program and want to divide them and receive a resulting number equivalent to the decimal value of that division. The C# language provides a division operator, which we use here, but the most common problem is related to casting the operands in the division correctly to get an accurate value.
This C# article shows aspects of division. The divide operator is covered.

Let's look at a C# program that demonstrates how you must cast the operands in a division operation to get a valid result that is not truncated to an incorrect size.
The program declares two Int32 variables and then divides the first by the second in five different operations. The operations are all different in the lexical syntax based on the casts used. The first and third division operations yield a value of zero, which isn't what we want.
Program that divides numbers [C#]
using System;
class Program
{
static void Main()
{
// Divide the first number by the second number.
int operand1 = 100;
int operand2 = 345;
// Incorrect division for double:
double number1 = operand1 / operand2;
Console.WriteLine(number1);
// Correct division for double:
double number2 = (double)operand1 / operand2;
Console.WriteLine(number2);
// Incorrect division for double:
double number3 = (double)(operand1 / operand2);
Console.WriteLine(number3);
// Correct division for double:
double number4 = (double)operand1 / (double)operand2;
Console.WriteLine(number4);
// Correct division for double:
double number5 = operand1 / (double)operand2;
Console.WriteLine(number5);
}
}
Output
0
0.289855072463768
0
0.289855072463768
0.289855072463768
Cast divisions. In the program you can see that the first and third double variables will be assigned to the value of zero. In the first and third assignments, there were no casts on the integers or the entire division was casted at once. To properly coerce the intermediate language to have the correct casts, you must cast either operand separately, as in number2 and number5, or both operands. When either operand or both operands are casted to double, the output of the program is 0.289855072463768.
Operators as functions with parameters. The C# language specification provides the definition of all the predefined (built-in) operators in the language. It describes these predefined operators in the same way as real methods. So you can think of the two operands on each division expression as parameters to a method with a custom syntax.

Role of C# compiler. In this program, the C# compiler will remove the cast from the number3 assignment completely in the intermediate language output. For the three assignments where one or both operands are casts, it will insert casts to both operands in each case. The complexity of this situation is mainly because of the rules by which the C# compiler interprets casts in the source code.
There exists the concept of numeric promotion in the C# language. This rule is that one when parameter to an operator or method can be implicitly converted to match the operator or method signature, all the parameters will be promoted to match the signature.
This is a trick the C# compiler uses to ensure more programs will compile when they seem logical. However, this can lead to incorrect behavior in some cases. Unfortunately, numeric promotion does not involve getting big pay raises from your boss.
Numeric Promotion
There exists a modulo operator that implements remainder division. This should remind you of doing long division in school, where you must compute the "leftover" part after dividing two numbers. This site has more information on the modulo operator.
Modulo OperatorWe saw how you can divide two integers in the C# language, using the casting syntax to ensure that the result has the correct number of decimal places and is not truncated. You need to cast one or both operands separately to indicate that you want the expression evaluation to return a double type, which will provide more decimal places and be more accurate.
Number Examples