Back to Blog
C#

C# int to double conversion: implicit conversion, casting, and Convert.ToDouble

Learn how to convert int to double in C# using implicit conversion, explicit casting, and Convert.ToDouble, and how to avoid the integer division pitfall.

C#type conversioncastingConvert.ToDoubleinteger division
Diagram showing int to double conversion in C# with implicit, cast, and Convert methods.

In C#, you can convert an int to a double with an implicit conversion, an explicit cast, or Convert.ToDouble. The approaches differ in where they are useful: the implicit conversion is automatic for assignments and method calls, an explicit cast is the standard way to force floating-point arithmetic such as division, and Convert.ToDouble helps when the source value is not statically typed. The most common reason this conversion matters is the integer division pitfall: dividing two int values in C# truncates the result unless at least one operand is converted to double.

Implicit conversion from int to double

C# defines an implicit conversion from int to double because every int value fits exactly in a double's 53-bit mantissa. The compiler inserts the conversion automatically when you assign an int to a double variable or pass an int where a double is expected.

int count = 42; double value = count; // implicit conversion

No data is lost for int values, so this conversion is safe and requires no explicit syntax. It also applies when you call a method that expects a double:

double result = Math.Sqrt(count); // count is implicitly converted

Explicit casting with (double)

An explicit cast is used when the compiler would otherwise perform integer arithmetic. The classic case is division: dividing two ints yields an int, truncating any fractional part. To get a double result, cast at least one operand to double before the division.

int numerator = 7; int denominator = 2; double wrong = numerator / denominator; // 3.0 (integer division first) double correct = (double)numerator / denominator; // 3.5

The cast (double)numerator converts the int to a double, so the division is performed in floating-point. You can cast either operand; the other is implicitly converted. Explicit casting is also useful when you want the conversion to be visible in mixed-type expressions.

Using Convert.ToDouble

Convert.ToDouble is a static method that converts various types, including int, to double. It is more verbose than a cast, but it is useful when the source is not statically typed, such as when reading from a collection of objects or parsing a value at runtime.

int number = 123; double converted = Convert.ToDouble(number);

Convert.ToDouble also handles strings that represent numbers, which a cast cannot do. For a known int, however, the implicit conversion or a cast is simpler and more idiomatic. If the source is a string, be aware that Convert.ToDouble(string) uses the current culture by default, which can cause unexpected results in international applications. A cast from int to double is culture-invariant.

The integer division pitfall

The most common reason developers search for int to double conversion is to fix integer division. When both operands are int, C# performs integer division, discarding the remainder. This is not a bug but a language rule that surprises many.

int total = 10; int count = 4; double average = total / count; // 2.0, not 2.5

To get a floating-point result, convert at least one operand to double:

double average = (double)total / count; // 2.5

Alternatively, you can use total * 1.0 / count, but the explicit cast is the standard, readable solution.

Precision and rounding behavior

Double is a binary floating-point type. It can represent every int value exactly, but it cannot represent all decimal fractions exactly. For example, 0.1 is not stored precisely. This matters when you convert an int to double and then perform arithmetic that involves fractions.

int cents = 1; double dollars = cents / 100.0; // stored as approximately 0.01

If you need exact decimal arithmetic, consider decimal instead of double. The conversion from int to decimal is also implicit and preserves the integer value exactly. Use double when the approximate nature of floating-point is acceptable, such as in scientific calculations or graphics.

Performance and readability

For a known int, the implicit conversion and explicit cast are direct and do not require the helper method call that Convert.ToDouble adds. In ordinary application code, the performance difference is usually negligible; readability and intent matter more.

Use implicit conversion when you are assigning an int to a double variable and no arithmetic is involved. Use an explicit cast when you need to force floating-point arithmetic, especially in division. Use Convert.ToDouble when you are dealing with non-typed input, such as object or string, where a direct cast from that input type to double is not available or would first require converting the value to a known numeric type.

Choosing the right approach

The decision between these methods depends on the context:

ScenarioRecommended Approach
Assign int to double variableImplicit conversion
Force floating-point divisionExplicit cast on one operand
Convert from object or stringConvert.ToDouble
Write clear, intent-revealing codeExplicit cast when arithmetic is involved

Avoid using Convert.ToDouble for a known int unless you are already handling multiple types. The cast is more idiomatic and makes the conversion explicit.

When you need to round the result to a specific number of decimal places, use Math.Round after the conversion. The default rounding mode is banker's rounding (MidpointRounding.ToEven). For example:

double value = Math.Round((double)total / count, 2);

This returns a double rounded to two decimal places, but the underlying binary representation may still be slightly off. If you need a specific decimal display, format the value with a format string.

The implicit conversion from int to double is a fundamental part of the C# type system. Understanding when to rely on it and when to use an explicit cast prevents subtle bugs, especially in arithmetic expressions. The key is to recognize that integer division is the main trap and that a single cast on one operand solves it.

C# int to double conversion: implicit conversion, casting, and Convert.ToDouble | RYUSLOG DEV