Home
Map
Divide Number ExamplesUnderstand the division operator, and evaluate casts for dividing numbers.
C#
This page was last reviewed on Sep 7, 2023.
Divide. In C# division uses the forward-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 2 ints. But if we do not cast an int in the expression, the result may be truncated (and not useful for us).
Cast, Int
Bitwise Divide
Syntax chart. Consider a number like 100. We want to divide it by 345, and we must get a fractional double result. We must use a cast on the numerator (or denominator).
(double)100 / 345 = ... 100 / (double)345 = ...
This program declares 2 Int32 variables and then divides the first by the second in 5 different operations. The operations differ in lexical syntax.
Detail In the first and third assignments, there were no casts on the integers or the entire division was cast at once.
double
Tip To coerce the intermediate language to have the correct casts, we cast either operand (number 2 and number 5) or both operands.
And The neither operand or both operands are cast to double, the output is approximately 0.29.
using System; // 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);
0 0.289855072463768 0 0.289855072463768 0.289855072463768
Discussion. The C# language specification provides the definition of all its predefined operators. It describes these predefined operators in the same way as real methods.
So You can think of the 2 operands on each division expression as parameters to a method with a custom syntax.
Info The C# compiler removes the cast from the number 3 assignment. If one or both operands are casts, it inserts casts to both operands.
Note The complexity of this situation is mainly because of the rules by which the C# compiler interprets casts in the source code.
In numeric promotion, a parameter to an operator or method can be implicitly converted to match the operator or method signature. The C# compiler does this to help more programs compile.
Modulo. A modulo operator implements remainder division. This might remind you of doing long division in school, where you must compute the "left over" part after dividing 2 numbers.
Modulo
A summary. We used the casting syntax in division to ensure the correct number of decimal places. You need to cast one or both operands to get a double type.
int, uint
Cast
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.