• Register
1 vote
111 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

15.3k points

Please log in or register to answer this question.

1 Answer

0 votes

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.

94.3k points