Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# Divide Numbers

This C# article shows aspects of division. It covers the divide operator. It uses casting to improve results.

Division uses the slash character.

With division, one common problem is related to correctly casting the operands in the division. With correct syntax we get a double from the division of two ints.

Cast to IntInt

Example. The program declares two Int32 variables and then divides the first by the second in five different operations. The operations differ in lexical syntax. The first and third operations yield a value of zero, which isn't what we want.

Here: This program shows how you must cast the operands in a division operation to get a valid result that is not truncated.

C# program that divides numbers

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;

	// Correct division for double:
	double number2 = (double)operand1 / operand2;

	// Incorrect division for double:
	double number3 = (double)(operand1 / operand2);

	// Correct division for double:
	double number4 = (double)operand1 / (double)operand2;

	// Correct division for double:
	double number5 = operand1 / (double)operand2;



In this 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 cast at once.


Tip: To coerce the intermediate language to have the correct casts, we cast either operand (number2 and number5) or both operands.

And: When either operand or both operands are cast to double, the output is approximately 0.29.

Discussion. 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.

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.

Note: The complexity of this situation is mainly because of the rules by which the C# compiler interprets casts in the source code.

Numeric promotion. In numeric promotion, a 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.

Numeric Promotion

Note: 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. It does not involve getting big pay raises from your boss.

Modulo. A modulo operator 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.


Summary. We used the casting syntax in division to ensure the correct number of decimal places. You need to cast one or both operands separately to indicate that you want the expression evaluation to return a double type.