HOWTO · Csharp

How to Get Double Value by Dividing Two Integers in C#

Learn why C# integer division truncates 30 / 7 to 4 and how to convert an operand before division to get a fractional double quotient.

On this page

In C#, the result of dividing two integers is an integer. To keep the fractional part of a quotient, convert at least one operand to double before the division takes place. This distinction matters when an expression is assigned to a floating-point variable: the assignment happens after the division and cannot restore digits already discarded by integer arithmetic.

Why 30 / 7 returns 4 for integer operands

The / operator uses the types of its operands to select the arithmetic operation. When both operands are int, C# performs integer division. For positive values, it discards the fractional part, so 30 / 7 evaluates to 4, not 4.285714.... For signed integer operands, truncation is toward zero; it is not rounding to the nearest integer.

The same rule applies if the destination variable is a double. In double quotient = 30 / 7;, the division is evaluated first as integer division, producing 4; that integer is then converted to 4.0 for assignment. Converting the completed expression, as in (double)(30 / 7), has the same limitation. Neither form can recover the fractional part after the integer operation has discarded it.

Convert an operand before dividing to get a double result

Convert one operand before applying /. The cast (double)n1 changes the type of that operand, so the division uses floating-point arithmetic and returns a double. The other operand is converted for the operation as well. Keeping n1 and n2 as integers is fine; only the expression needs a floating-point operand.

Run this example with the two integer inputs shown:

using System;

public class DivisionExample
{
    public static void Main()
    {
        int n1 = 30;
        int n2 = 7;

        int integerQuotient = n1 / n2;
        double castQuotient = (double)n1 / n2;
        decimal decimalQuotient = (decimal)n1 / n2;
        double convertedQuotient = decimal.ToDouble(decimalQuotient);

        Console.WriteLine($"Integer division: {integerQuotient}");
        Console.WriteLine($"Cast before division: {castQuotient}");
        Console.WriteLine($"Convert decimal quotient: {convertedQuotient}");
    }
}
Integer division: 4
Cast before division: 4.28571428571429
Convert decimal quotient: 4.28571428571429

The first line shows the truncated integer quotient. The cast and decimal paths retain a fractional quotient. The direct cast is the simplest choice when the inputs are int and the desired result type is double; cast either operand before division, not the quotient afterward. If the calculation must remain in a particular type, declare the result to match that type so later operations follow the intended arithmetic rules.

When to use decimal.ToDouble

decimal.ToDouble converts a value that is already of type decimal into a double. It is useful when an earlier calculation or input is decimal and a later API requires a double. It does not directly convert an int; first convert the integer operands to decimal and perform decimal division, as the example does, then pass that decimal quotient to decimal.ToDouble.

For two integer inputs where the goal is simply a fractional quotient, this route adds an extra conversion and is usually unnecessary. Prefer (double)n1 / n2 for that case. Choose decimal arithmetic only when its base-10 representation and range fit the application; it does not make every possible value or calculation exact. A double also has finite precision, so many fractional values are represented approximately. Select the numeric type based on the precision and range the calculation needs, and do not assume that changing the result variable alone changes the arithmetic used by its expression.