How to Convert Float to int in C++? Last Updated : 21 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example: Input:float val1 = 3.14;float val2 = 9.99;Output:Integer for float value 3.14 is: 3Integer for float value 9.99 is: 10Converting From Float to int in C++ To convert a floating value to an int value, first use the std::round() function that rounds the float to the nearest whole number, and then we can use the static_cast operator to cast the result to an integer. Syntax to Convert Float to int in C++ int intValueName = static_cast<int>(round(floatValueName)); C++ Program to Convert Float to intThe below program demonstrates how we can convert a floating value to an integer in C++. C++ // C++ program to convert float to int using std::round and // casting #include <cmath> #include <iostream> using namespace std; int main() { // Define float values float floatValue1 = 3.14; float floatValue2 = 9.99; // Use std::round and static_cast to convert the float // values to int type int intValue1 = static_cast<int>(round(floatValue1)); cout << "The integer for float value " << floatValue1 << " is " << intValue1 << endl; int intValue2 = static_cast<int>(round(floatValue2)); cout << "The integer for float value " << floatValue2 << " is " << intValue2 << endl; return 0; } OutputThe integer for float value 3.14 is 3 The integer for float value 9.99 is 10 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Convert Float to int in C++? H heysaiyad Follow Improve Article Tags : C++ Programs C++ CPP Examples Practice Tags : CPP Similar Reads Convert Float to String In C++ In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into 3 min read How to Convert wstring to int in C++? In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr = 2 min read How to Convert Char Array to Int in C++ In C++, a character array is treated as a sequence of characters also known as a string. Converting a character array into an integer is a common task that can be performed using various methods and in this article, we will learn how to convert char array to int in C++. Example Input:char arr1[] ="1 2 min read How to Convert wstring to double in C++ In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou 2 min read How to Catch Floating Point Errors in C++? In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at 2 min read Convert String to Integer Vector in C++ Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve 2 min read C++ Program to Convert Scientific Notation to Decimal Here, we will see how to convert scientific notations to decimals using a C++ program. Example: 1e9 = 1000000000 1e9 = 1000000000.000000 Calculate power using the pow(a,b) function Header File used: <math.h> pow also referred to as the power of function. Here, pow(a,b) refers to a power b. Let 3 min read How to Round a Double to an int in C++? In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double valu 2 min read How to Handle Wrong Data Type Input in C++? In C++, when weâre expecting a certain type of input but the user enters a different type then such input is called incorrect data input and it can cause issues like unexpected behavior, program failures, or inaccurate results. In this article, we will learn how to handle wrong data type input in C+ 2 min read Different Ways to Convert Hex String to Integer in C++ STL A hexadecimal number is a number whose base is 16. has numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. And 10, 11, 12, 13, 14, and 15 these numbers are denoted by A, B, C, D, E, F. In C++ STL there are certain properties that help to convert a hexadecimal string or number to a dec 5 min read Like