convert double to int
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
This is truncation toward zero. That last detail matters for negative numbers.
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:
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'
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:
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:
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
doublemay 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
doubletointusually 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.
Related reading
- Convert normal recursion to tail recursion with multiple recursive calls
- Converting JS object to ArrayBuffer to transfer to/from web worker equals bottleneck
- Converting loop to tasks
- Converting pixels to dp
- Convolutional Neural Network seems to be randomly guessing
- cool project to use a genetic algorithm for?
- Core Data background context best practice
- Correlation among Hyperparameters of Classifiers

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.