HOWTO · C++
How to Convert Float to Int in C++
This article introduces how to convert a float to an int in C++.
On this page
To convert a C++ float to an int and discard its fractional part, use static_cast<int>(value). A floating-to-integer conversion truncates toward zero: 4.8f becomes 4, while -4.8f becomes -4. If you need a different rounding rule, choose a function from <cmath> before converting.
Use Direct Assignment to Convert Float to Int
Direct assignment converts a float to an int implicitly. The fractional part is discarded. Here is a compact program that assigns a familiar value and displays the converted integer:
#include <iostream>
int main() {
float value = 4.8f;
int result = value;
std::cout << result << '\n';
}
C++ conversion semantics give the integer value 4 for 4.8f. The example prints:
4
For negative inputs, truncation still moves toward zero, so -4.8f converts to -4 rather than the floor value -5.
Use C-style Cast to Convert Float to Int
A C-style cast writes the target type in parentheses, as in int result = (int)value;. For a scalar floating-point value, it produces the same truncation-toward-zero result as direct assignment and static_cast<int>(value), provided the converted value is representable as an int.
C-style casts can express several kinds of conversions, so their effect is less explicit at a glance. In modern C++, prefer static_cast<int>(value) when this is the conversion you intend. The C-style spelling remains useful to recognize in existing code, but it does not change how a floating-point value is truncated.
Use static_cast to Convert Float to Int
Use static_cast<int>(value) to make the conversion explicit and identify its destination type. For a floating-point value, static_cast truncates toward zero; it does not round to the nearest integer or check whether the result fits in int.
The conversion has undefined behavior if the truncated value cannot be represented by int. Checking that the original value is within the limits of int before converting is a conservative guard: it can reject some fractional values whose truncated result would still fit. static_cast does not clamp or validate the value. See the static_cast reference for the language rules and other conversions supported by the operator.
Use Rounding Strategies to Convert Float to Int
Choose a rounding function when truncation toward zero is not the desired result. Include <cmath> and apply the function before converting its result to int. The rounded value must also be representable as an int.
Rounding Down (Floor)
std::floor(value) returns the greatest integral value less than or equal to the input. For example, floor gives 4 for 4.8f and -5 for -4.8f. This differs from converting directly to int, which gives 4 and -4 respectively. To store a floor result as an integer, use static_cast<int>(std::floor(value)) after ensuring it is in range.
Rounding Up (Ceil)
std::ceil(value) returns the least integral value greater than or equal to the input. It gives 5 for 4.8f and -4 for -4.8f. Convert that result with static_cast<int>(std::ceil(value)) when an integer is needed and the result fits in int.
Rounding to the Nearest Integer
std::round(value) selects the nearest integral value, with halfway cases rounded away from zero. Thus std::round(4.5f) is 5, and std::round(-4.5f) is -5. Use static_cast<int>(std::round(value)) to obtain an integer, again checking that the rounded result is representable. This rule differs from truncation, floor, and ceil, especially for negative inputs.
Conclusion
For ordinary float-to-int conversion, static_cast<int>(value) makes the intent clear and truncates toward zero. Direct assignment and a C-style cast have the same truncation behavior for suitable values, but the named cast is easier to recognize as a deliberate C++ conversion. Use std::floor, std::ceil, or std::round from <cmath> when their respective rounding rules match the task. Before any conversion, ensure the value produced by truncation or rounding fits in the range of int; none of these casts checks or adjusts it for you.