type conversion
data types
programming
code optimization
integer conversion

convert double to int

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a double to an int sounds simple, but there are several different conversions hiding behind that phrase. You might want truncation, rounding, flooring, ceiling, or a safe conversion that rejects values outside the integer range.

Truncation Is the Default Cast

In many languages with double and int, a direct cast removes the fractional part instead of rounding to the nearest whole number. The examples below use C#, but the same idea appears in Java, C, and similar languages.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        double value = 3.99;
8        int result = (int)value;
9        Console.WriteLine(result); // 3
10    }
11}

This is truncation toward zero. That last detail matters for negative numbers.

csharp
double a = -3.99;
int b = (int)a;
Console.WriteLine(b); // -3

Some developers expect -4 here, but that would be Math.Floor, not a cast.

Use Rounding When You Mean Nearest Integer

If the goal is "closest whole number," use an explicit rounding function instead of a cast:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        double value = 3.5;
8        int rounded = (int)Math.Round(value);
9        Console.WriteLine(rounded);
10    }
11}

This communicates intent better than a cast. It also makes the choice of rounding strategy visible in code.

Floor and Ceiling Are Different Operations

Sometimes you need a specific direction:

  • 'Math.Floor(x) rounds down toward negative infinity'
  • 'Math.Ceiling(x) rounds up toward positive infinity'
csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        double value = -3.2;
8        Console.WriteLine(Math.Floor(value));   // -4
9        Console.WriteLine(Math.Ceiling(value)); // -3
10    }
11}

These are not interchangeable with casting. Choosing the wrong one can create subtle bugs in indexing, pagination, pricing, or time calculations.

Watch for Overflow

An int has a much smaller range than a double. If the floating-point value is larger than the integer range, conversion can fail or produce the wrong result depending on language and context.

A defensive pattern is to check the range first:

csharp
1using System;
2
3class Program
4{
5    static int SafeToInt(double value)
6    {
7        if (value > int.MaxValue || value < int.MinValue)
8            throw new OverflowException("Value is outside the Int32 range.");
9
10        return (int)value;
11    }
12}

This is more important than it looks. Production bugs from numeric conversion are often overflow bugs, not rounding bugs.

Floating-Point Precision Still Matters

Remember that double values are approximations. A value that prints as 2.0 might internally be slightly above or below an integer boundary after several calculations. That affects both comparisons and conversions.

For example, if a calculation is expected to produce a whole number but might contain tiny floating-point noise, it is usually better to round deliberately before converting:

csharp
double value = 9.999999999;
int result = (int)Math.Round(value);

Whether that is correct depends on the domain. The main point is to make the choice explicit instead of hoping a cast happens to do the right thing.

Performance Versus Correctness

Casting is cheap, but it is only correct when truncation is actually what you want. In most real applications, clarity and correctness matter more than shaving a tiny amount of conversion time off a single numeric operation.

If the meaning is "drop the fraction," cast. If the meaning is "nearest whole number," round. If the meaning is "must fit in range," guard the conversion.

Common Pitfalls

  • Using a cast when the real requirement was rounding.
  • Forgetting that casts truncate toward zero, especially for negative numbers.
  • Ignoring overflow when the double may exceed integer range.
  • Assuming floating-point results are exact before conversion.
  • Optimizing for micro-performance instead of choosing the correct conversion rule.

Summary

  • A direct cast from double to int usually truncates the fractional part.
  • Truncation, rounding, floor, and ceiling are different operations.
  • Negative numbers make those differences more obvious.
  • Safe conversion should check that the value fits in the target integer range.
  • Choose the conversion based on meaning, not just on convenience.

Course illustration
Course illustration

All Rights Reserved.