# How to convert double to int c++

1 vote
64 views

## Problem:

I am willing to know the typecasting procedure in c++. What is the way to convert a double to an integer in c++? A sample code would be much appreciated.

Thanks

## Solution:

Like some of the other programming languages typecasting are possible and quite easy to do.

As far as your query is a concern, you asked to know the double-integer casting in c++

Either in Explicit Conversion:

``````#include <iostream>
using namespace std;

int main()
{
double x = 1.5;

// Explicit conversion from double to int
int sum = (int)x + 1;

cout << "Sum = " << sum;

return 0;
} ``````

Or in Cast Operator:

``````#include <iostream>
using namespace std;
int main()
{
float f = 3.5;

// using cast operator
int b = static_cast<int>(f);

cout << b;
} ``````

Above both programs will return an integer value. I hope you get this.

## Related questions

20 views
Problem: Let&rsquo;s say that the double variable x holds the value 3.6987 and needs to be converted to an int. There are two ways that this can be done: Which type of methods use for converting double type into int type? Given a Double real number, the task is to convert it into Integer in Java. Examples: Input: double = 3452.234 Output: 3452 Input: double = 98.23 Output: 98
12 views
Problem: Hello Kodlogs, I am a beginner of c++ programming, as well as a new member of this forum. I am having an issue with typecasting. I wonder if I can convert a decimal value to hexadecimal using a c++ program. How can I do it? Could you please suggest me an easy way to do that? I appreciate your effort a lot. Thanks.
14 views
Problem: I have two integers a and b. I need to calculate a/b and as outcome I would like to get float. For example as an outcome of 7/2 I would like to have 3.5. Could you please help me with that?
186 views
Problem: Is there any way to convert integer value to bool value? How can I assign true or false to a bool variable based on user input?
18 views
Problem: I need to convert a float value taken input from user to int value for calculation. How can I do that? Any suggesstions?
13 views