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

12 7 7
15,250 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.

13 9 6
94,240 points

Related questions

0 votes
1 answer 11 views
11 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
asked Dec 3, 2020 Mashhoodch 5.6k points
0 votes
1 answer 7 views
7 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.
asked Jun 24, 2020 adamSw 11.3k points
0 votes
1 answer 7 views
7 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?
asked Oct 9, 2020 kalyani456 280 points
0 votes
1 answer 3 views
3 views
Problem: I have a double (call it x), meant to be 55 but in actuality stored as 54.999999999999943157 which I just realised. So when I do double x = 54.999999999999943157; int y = (int) x; y = 54 instead of 55!
asked Dec 13, 2020 Mashhoodch 5.6k points
0 votes
1 answer 85 views
85 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?
asked Oct 18, 2020 Code Learner 5.1k points
0 votes
1 answer 10 views
0 votes
1 answer 14 views
14 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?
asked Oct 11, 2020 Code Learner 5.1k points
0 votes
1 answer 5 views
5 views
Help me out to know how to convert integer to string using c++.
asked Dec 7, 2020 TeamScript 9.2k points
0 votes
1 answer 11 views
0 votes
1 answer 10 views
10 views asked Oct 27, 2020 xgtx 320 points