Back to Blog
C#

C# int to double conversion: implicit, cast, and Convert

c# int to double conversion: Learn how to convert int to double in C# using implicit conversion, explicit casting, and Convert.ToDouble, including common pitfalls like...

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

c# int to double conversion requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.

When you need to convert an int to a double in C#, you have three main options: rely on implicit conversion, use an explicit cast, or call Convert.ToDouble. Each behaves differently in certain contexts, and the choice affects readability and correctness, especially when integer division is involved.

Implicit Conversion from int to double

C# defines an implicit conversion from int to double because every int value can be represented exactly as a double. 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 because the range of int is well within the range of double, and the precision of double (53-bit mantissa) is more than enough to represent all 32-bit integers exactly. This conversion is safe and requires no explicit syntax.

The implicit conversion 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 required 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, you must cast at least one operand to double before the division.

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

The cast (double)numerator converts the int to a double, causing the division to be performed in floating-point. You can cast either operand; the other is implicitly converted.

Explicit casting is also useful when you want to make the conversion visible in code, even where implicit conversion would work. Some developers prefer it for clarity 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. However, for a known int, the cast or implicit conversion is simpler and avoids the overhead of a method call. In most performance-sensitive code, the cast is preferred because it is a direct IL instruction, while Convert.ToDouble may involve additional checks.

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, not 2.5

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

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

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

Precision and Rounding Behavior

Double is a binary floating-point type. While it can represent all int values exactly, 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; // 0.01, but stored as 0.010000000000000002

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

Performance and Readability

From a performance perspective, the implicit conversion and explicit cast are essentially free. They are compiled to a single IL instruction (conv.r8 for int to double). Convert.ToDouble is a method call that may involve boxing or type checks, so it is slower in tight loops. However, in typical application code, the difference is negligible. The more important factor is readability and intent.

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 cast is not possible.

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 in a code path that handles multiple types. The cast is more idiomatic and performs better. Also be aware that Convert.ToDouble uses the current culture for string parsing, which can cause unexpected results in international applications. A cast is culture-invariant.

When you need to round the result to a specific number of decimal places, apply Math.Round after the conversion, but be aware of the rounding mode (banker's rounding by default). For example:

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

This gives you a double that is rounded to two decimal places, but the underlying representation may still be slightly off. If you need exact decimal output, format the value using a format string.

The implicit conversion from int to double is a fundamental part of 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: Practical Usage and Code Exampl | RYUSLOG DEV